//This is the main js file which has all common functions used in this application
//This document ready function is common for all pages in this application so take care of objects id and name

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function loading( div_id )  {
	$("#" + div_id).hide();
	$("#div_loading").show();
	$("#div_loading").html('<img src="images/loading.gif" ></img>');
	document.body.style.cursor = 'wait';
}

function loaded( div_id )  {
	$("#" + div_id).show();
	$("#div_loading").hide();
	document.body.style.cursor = 'default';
}

var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

// open hidden layer
function mopen(id)  {	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';
}
// close showed layer
function mclose()  {
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()  {
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()  {
	if(closetimer)	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 

var url = 'ajaxPage.php';
$(document).ready(function() {

	//header search text
	$("#query").focus(function() {
		$("#query").val('');
	});
	$("#query").blur(function() {
		( $("#query").val() == '' ) ? $("#query").val('Search') : '';
	});
	
	//Static pages Starts here
	
	$("[name=staticpages]").click(function() {
		var ck_name = $("#hide_val").val();
		var ck_val = $(this).attr('id');
		$.cookie( ck_name, ck_val);
	});
	
	$('#gallery a').lightBox();
	
	//Static pages Ends here
	
	//Products Page Starts Here
	
	//slides the element with class "menu_body" when paragraph with class "menu_head" is clicked 

	$("#leftmenu h2").click(function() {
		$(this).next("#pchilds").slideToggle(500).siblings("#pchilds").slideUp("slow");
		return false;
	});
	
	$(".product_group a#a_pdtGrp").click(function() {
		var prsnt = $(this).next("[name=divacitem]").css('display');
		$("[name=divacitem]").slideUp("slow");
		( prsnt == 'none' ) ? $(this).next("[name=divacitem]").slideDown("slow") : '';
		return false;
	});
	
	$("#lipdtInfo a").click(function() {
		$(this).next("[name=spdtInfo]").slideToggle("slow");
		return false;
	});

	//Products Page Ends Here

	//Projects Page Starts Here
	
	//to show projects inn description div
	var curPjt = 0;
	$("[name=pjtTitle]").click(function() {
		var clkPjt = $(this).attr('id');
		clkPjt = clkPjt.replace('pjt', '');

		if( clkPjt !== curPjt )  {
			loading('div_project_des');
			$.get(url, {qs:'project', p_id:clkPjt}, function(data) {
				eval(data);
				$("#h1_pjt_desc").html(pjtJsn['tit']);
				$("#img_pjt_desc").attr('src', pjtJsn['img']);
				$("#p_pjt_desc").html(pjtJsn['desc']);
				//(pjtJsn['link']) ? $("#lnk_pjt_desc").html('<a href="'+ pjtJsn['link'] + '" >'+ pjtJsn['tit'] +'</a>') : $("#lnk_pjt_desc").html('');
				loaded('div_project_des');
			});
			curPjt = clkPjt;
		} 
	});
	
	$("[name=pjtTImg]").click(function() {
		$(this).next().children(0).trigger('click');
	});

	//Projects Page Ends Here
	
	//News page starts here
	
	$("#firstpane p.menu_head2").click(function() {
		$(this).css({backgroundImage:"url(images/down.png)"}).next("div.menu_body").slideToggle(500).siblings("div.menu_body").slideUp("slow");
		$("#firstpane p.menu_head2").css({backgroundImage:"url(images/left.png)"});
		$(this).css({backgroundImage:"url(images/down.png)"});
	});

	//News page Ends here
	
	//History page starts
	
	$("#f_div p.menu_head2").click(function() {
		$(this).css({backgroundImage:"url(images/down.png)"}).next("div.menu_body").slideToggle(500).siblings("div.menu_body").slideUp("slow");
		$("#f_div p.menu_head2").css({backgroundImage:"url(images/left.png)"});
		$(this).css({backgroundImage:"url(images/down.png)"});
	});
	
	$("#s_div p.menu_head2").click(function() {
		$(this).css({backgroundImage:"url(images/down.png)"}).next("div.menu_body").slideToggle(500).siblings("div.menu_body").slideUp("slow");
		$("#s_div p.menu_head2").css({backgroundImage:"url(images/left.png)"});
		$(this).css({backgroundImage:"url(images/down.png)"});
	});
	
	$("#t_div p.menu_head2").click(function() {
		$(this).css({backgroundImage:"url(images/down.png)"}).next("div.menu_body").slideToggle(500).siblings("div.menu_body").slideUp("slow");
		$("#t_div p.menu_head2").css({backgroundImage:"url(images/left.png)"});
		$(this).css({backgroundImage:"url(images/down.png)"});
	});

	$("[id$=_option]").click(function() {
		var eleId = $(this).attr('id').split('_')[0];
		$("[id$=_div]").hide();
		$("#"+ eleId + "_div").show();
		$("#listtabs .active").removeClass('active');
		$("#"+ eleId +"_li").addClass('active');
	});
	/*$("#f_option").click(function() {
		$("#s_div").hide();
		$("#t_div").hide();
		$("#f_div").show();
		
		$("#listtabs .active").removeClass('active');
		$("#f_li").addClass('active');
	});
	
	$("#s_option").click(function() {
		$("#t_div").hide();
		$("#f_div").hide();
		$("#s_div").show();
		
		$("#listtabs .active").removeClass('active');
		$("#s_li").addClass('active');
	});
	
	$("#t_option").click(function() {
		$("#f_div").hide();
		$("#s_div").hide();
		$("#t_div").show();
		
		$("#listtabs .active").removeClass('active');
		$("#t_li").addClass('active');
	});*/
	
	//History page ends
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
});
