function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}
function getElementPosition(elemId)
{
    var elem = document.getElementById(elemId);
    //var w = elem.offsetWidth;
    //var h = elem.offsetHeight;
    var l = 0;
    var t = 0;
    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return {"left":l, "top":t};//, "width": w, "height":h};
}
function getEl(ElName,number)
{
	if (number === undefined) number = 0;
	return document.getElementsByName(ElName)[number];
}
function getElValue(ElName,number)
{
	getEl(ElName,number).value;
}
function change_select(elName,load)
{
	select = getEl(elName);
	if (load == undefined) {
		select.style.color='#000000'
	}
	if (elName == 'place_buying') {
		place_change(select);
	}
}
function set_tip_position(tipId)
{
	question = document.getElementById(tipId+"_question");
	tip = document.getElementById(tipId+"_tip");
	tip_pos_left = $(question).offset().left+$(question).width()+1 - $(tip).parent().offset().left;
	tip_pos_top = $(question).offset().top+($(question).height()/2)-($(tip).height()/2) - $(tip).parent().offset().top;
	tip.style.left=tip_pos_left+"px";
	tip.style.top=tip_pos_top+"px";

}

function toggleTip() {
	elemId = $(this).attr('id').replace('_question', '');
	Tip = $('#' + elemId + '_tip');
	if ($(this).hasClass('active-question') == false) {
		set_tip_position(elemId);
		Tip.show('fast');
	} else {
		Tip.hide('fast');
	}
	$(this).toggleClass('active-question');
}

function getMaxHeight()
{
	h = $('div.middle-box-head.slide-box-head:first').next('div').height();
	max_h = parseInt(h);
	cont_h = $("#faq-container").height();
	other_divs = $("#faq-container").children('div.middle-box-head.slide-box-head:not(:first)');
	for (i = 0; i<other_divs.length; i++)
	{
		div = $(other_divs[i]);
		if (parseInt(div.next('div').height()) > max_h)
		{
			max_h = parseInt(div.next('div').height());
		}
	}
	$("#faq-container").height(parseInt(cont_h)+max_h-parseInt(h));
}
function openRememberedBoxes()
{
	var product_boxes = $($('#product-main-box').children('.big-box-body')[0]).children(".slide-box-head");
	for (i = 0; i<product_boxes.length; i++)
	{
		product_box = $(product_boxes[i]);
		if ($(product_box).hasClass("head-active") == true && readCookie($(product_box).attr("id")) == null)
		{
			createCookie($(product_box).attr("id"),1,30);	
		}
		if (readCookie($(product_box).attr("id")) == '1')
		{
			if ($(product_box).hasClass("head-active") != true)
			{
				$(product_box).addClass("head-active").next('div').show();
			}
		}
		else
		{
			$(product_box).removeClass("head-active").next('div').hide();
		}
	}

	var active_faq_box = $('#faq-container').children("div.head-active")[0];
	var faq_boxes = $('#faq-container').children(".slide-box-head");
	for (i = 0; i<faq_boxes.length; i++)
	{
		faq_box = faq_boxes[i];
		if (readCookie($(faq_box).attr("id")) == '1')
		{
			$(active_faq_box).removeClass("head-active").next("div").hide();
			$(faq_box).addClass("head-active").next("div").show();
			break;
		}
	}
	
}
$(document).ready(function(){
	h = $("div.active_body").height();
	if (h != '0')
	{
		$("div.active_body").height(h+1);
	}
	getMaxHeight();
	
	openRememberedBoxes();

	$("#home-box > .big-box-head.big-head").click(function () {
		if (!$(this).hasClass('active-head')) {
			var body_id = $(this).attr("id").substr(0,$(this).attr("id").indexOf("_head"));
			$(".active_body").slideUp("fast", function () {
				$("#" + body_id + "_head").siblings(".big-box-head.big-head").removeClass("active-head");
				$("#" + body_id + "_head").toggleClass("active-head");
				$(this).removeClass("active_body");
				$("#" + body_id).slideDown('fast', function () {
					$(this).toggleClass("active_body").height($(this).css("height", "auto").height() + 1)
				});
			});
		}
	});
	
	$('.product_info_main span').click(function () {
		parent_div = $(this).parents('div');
		if ($(parent_div[0]).hasClass('product_box_close_index'))
		{
			$(this).parents('.product_box_close_index').siblings("div.product_info_index").slideUp("fast", function () {
				$(this).prev(".product_box_open_index").addClass("product_box_close_index").removeClass("product_box_open_index");
			});
			$(this).parents('.product_box_close_index').addClass("product_box_open_index").removeClass("product_box_close_index").next("div").slideDown("fast");
		}
		else
		{
			href = $(parent_div[0]).next().children().children().attr("href");
			window.location = href;
		}
	});
	$('.product_descr_close span').click(function () {
		$(this).parents('.product_descr_close').toggleClass("product_descr_open").next('.product_descr_box').slideToggle('fast');
	});
	$('div.middle-box-head.slide-box-head').click(function () {
		if ($(this).parent().attr('id') == 'faq-container') {
			if ($(this).hasClass('head-active') != true) {
				active_faq_box = $("div.head-active");
				eraseCookie($(active_faq_box).attr("id"));
				createCookie($(this).attr("id"),1,30);

				$(active_faq_box).next("div").slideUp("fast", function () {
					$(this).prev('div').removeClass("head-active");
				});
				$(this).addClass("head-active").next("div").slideDown("fast");
			}
		} else {
			if ($(this).hasClass("head-active") != true) {
				createCookie($(this).attr("id"),1,30);
				$(this).toggleClass("head-active").next('div').slideToggle("fast");
			} else {
				createCookie($(this).attr("id"),0,30);
				$(this).next('div').slideToggle("fast", function () {
					$(this).prev('div').toggleClass("head-active");
				});
			}
		}
	});
	
	$('a[id|="clear"][id|="confirm"][id|="edit"][id|="thanks"]').click(function (event) {
		event.preventDefault();
		$('#request').val($(this).attr('id').replace('-button', ''));
		$('form[name="request_form"]').submit();
	});
	
	$('input#close_popup').click(function () {
		close_faq_popup();
	});
	$(".faq_popup_frame a").click(function () {
		href = $(this).attr('href');
		window.parent.location = href;
	});
	
	$('.question-button').mouseover(toggleTip);
	$('.question-button').mouseout(toggleTip);
	
	$(window).keypress(function (event) {
		if (event.keyCode == 27)
		{
			window.parent.popup.hide();
		}
	});
});
function close_faq_popup()
{
	$('#popup_div').remove(); 
	$('#shadow_div').remove();
}
function show_faq_popup(h)
{
	$("#popup_frame").attr('height',parseInt(h));
	$("#popup_div").animate({height: h+69}, 'normal');
	//$("#popup_div").height(h+69);
}