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 change_select() {
	$(this).css("color", "#000000");
	if ($(this).attr("name") == 'place_buying') {
		place_change();
	}
}
function set_tip_position(tipId) {
	question = $('#' + tipId + '_question');
	tip = $('#' + 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.css('left', tip_pos_left + "px");
	tip.css('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;
		}
	}
	
}

function copyFilePath(event) {
	fromElement = event.target;
	var val = $(fromElement).val();
	if (val.indexOf('C:\\fakepath\\') >= 0) {
		val = val.substr(12);
	}
	if (val != '') {
		$(fromElement).next('.request_input_fake_file').children('input:first').val(val);
	}
}
function initFileUploads(input) {
	input = input || $('input:file');
	if ($.browser.msie) {
		input.attr("size", 30);
	}
	input.next(".request_input_fake_file").children('a.button').click(function (event) {
		event.preventDefault();
		input.click();
	});
	input.each(function () {
		$(this).change(copyFilePath);
		$(this).mouseout(copyFilePath);
	});
}

var addFileField = function(event) {
	event.preventDefault();
	var outerObj = $('tr.request_file_row');
	var outerObjLength = outerObj.length;
	var lastOuterObj = $(outerObj[outerObjLength - 1]);
	var newOuter = lastOuterObj.clone();
	
	$('.request_input_error', newOuter).removeClass('request_input_error').addClass('request_input');
	$('input', newOuter).val('');
	initFileUploads($('input.hidden_file_input', newOuter));
	newOuter.insertAfter(lastOuterObj);
	$(".add-file-button", lastOuterObj).hide();
	$(".remove-file-button", lastOuterObj).show();
	$(".remove-file-button", newOuter).hide().click(removeFileField);
	$(".add-file-button", newOuter).show().click(addFileField);
}

var removeFileField = function (event) {
	event.preventDefault();
	var outerObj = $('tr.request_file_row');
	var outerObjLength = outerObj.length;
	var lastOuter = $(outerObj[outerObjLength - 1]);
	var preLastOuter = $(outerObj[outerObjLength - 2]);
	
	$('input', preLastOuter).val('');
	
	//change remove to add in prelast avialable field
	$(".remove-file-button", preLastOuter).hide();
	$(".add-file-button", preLastOuter).show();
	
	//delete last field
	lastOuter.remove();
}

var removeAllFilesFields = function (outerClass) {
	var firstOuterObj = $('tr.request_file_row:first');
	$('input', firstOuterObj).val('');
	$(".remove-file-button", firstOuterObj).hide();
	$(".add-file-button", firstOuterObj).show();
	$("tr.request_file_row").slice(1).each(function () {
		$(this).remove();
	});
}

$(document).ready(function(){
	h = $("div.active_body").height();
	if (h != '0') {
		$("div.active_body").height(h+1);
	}
	getMaxHeight();
	
	openRememberedBoxes();

	$("select").change(change_select).keyup(change_select).focus(change_select);

	$("#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);

				var inner_block = $(this).toggleClass("head-active").next('div');
				inner_block.slideToggle("fast");
				if (($.browser.msie) && ($.browser.version <= "7.0") && ($(this).closest('#question-box').length > 0)){
				    var new_width = $(this).closest('form').width();
				    if($.browser.version == "6.0"){
					new_width -= 2;
				    }
				    inner_block.width(new_width);
				}
			} else {
				createCookie($(this).attr("id"),0,30);
				$(this).next('div').slideToggle("fast", function () {
					$(this).prev('div').toggleClass("head-active");
				});
			}
		}
	});

	$('a[id|="clear"], a[id|="confirm"], a[id|="edit"], a[id|="thanks"]').click(function (event) {
		event.preventDefault();
		$('#form_action').val($(this).attr('id').replace('-button', ''));
		$('#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);
	
	$('#attach_file').click(function (e) {
		if ($('#attach_file').is(':checked')) {
			$('.request_file_row').show();
		} else {
			removeAllFilesFields();
			$('.request_file_row').hide();
		}
	});
	if ($('#attach_file:checked').size() > 0) {
		$('tr.request_file_row').show();
	}
	initFileUploads();
	$('.add-file-button').click(addFileField);
	$('.remove-file-button').click(removeFileField);
	
	$(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');
}
