var g_slide_number = 0;
var g_next_slide = Number(1);

/* Для специального JS-кода конкретных пректов */
function DecorateHeaderMenu()
{
//$("#header_menu #header_menu_items div.menu_item:last-child").addClass("menu_last_item");
}

function DecoratePhotos()
{
	// в контентной части
	$("#middle_content #middle_content_table img[class!=cap_img][class!=noStyle][class!=photoH1]").wrap("<div class='content_photo inline_block' />");
	$("#middle_content #middle_content_table div.content_photo").wrap("<div class='content_photo_wrapper inline_block' />");
	$("#middle_content #middle_content_table div.content_photo").after("<div class='content_photo_description' />");
	$("#middle_content #middle_content_table div.content_photo_wrapper").each( function()
	{
		$(this).children("div.content_photo_description").html($(this).children("div.content_photo").children("img[class!=catalogue_img]").attr("title"));
	});
	$("div.content_photo img[class!=catalogue_img][class!=photoH1]").attr("title", "");
	// в колонке с новостями
	$("#middle_right_news_items div.middle_right_news_item img").wrap("<div class='content_photo' />");
	// в колонке с доп. товарами
	$("#middle_right_additional img").wrap("<div class='content_photo' />");

	$("div.content_photo_wrapper div.content_photo img").css("margin", "0");

	$("p div.content_photo_wrapper div.content_photo img").parent().parent().parent().css("text-indent", "0");

	$("div.content_photo_wrapper div.content_photo img").each( function()
	{
		$(this).parent().parent().children("div.content_photo_description").css("width", ($(this).width() + 2) + "px");
		if($(this).css("float") == "left")
		{
			$(this).css("float", "none").parent().parent().removeClass("inline_block").addClass("float_left").css("margin-right", "10px");
		}
		if($(this).css("float") == "right")
		{
			$(this).css("float", "none").parent().parent().removeClass("inline_block").addClass("float_right").css("margin-left", "10px");
		}
	});
}

function DecorateTables()
{
	$("#middle_content #middle_content_table table").addClass("content_table");
	$("table.content_table tr:first-child th").addClass("content_table_th");
	$("table.content_table tr:first-child th:first-child").addClass("content_table_first_th");
	$("table.content_table tr:first-child th:last-child").addClass("content_table_last_th");
	$("table.content_table tr td").addClass("content_table_td");
	$("table.content_table tr td:first-child").addClass("content_table_first_td");
	$("table.content_table tr td:last-child").addClass("content_table_last_td");
	$("table.content_table tr:odd").addClass("content_table_tr_odd");
	$("table.content_table tr:even").addClass("content_table_tr_even");
}

function DecorateContentDocuments()
{
	$("#middle_content #middle_content_table a").each( function()
	{
		if($(this).hasClass("content_document_word"))
		{
			$(this).wrap("<div class='content_document_href' />");
			$(this).parent().wrap("<div class='content_document content_document_word' />");
			$(this).parent().before("<div class='content_document_title' />");
		}
	});
	$("div.content_document").each( function()
	{
		$(this).children("div.content_document_title").html($(this).children("div.content_document_href").children("a").html());
		$(this).children("div.content_document_href").children("a").html("Скачать");
	});
}

function DecorateCatalogue()
{
	$("#middle_left_cataloque_menu_items div.active_system_menu_item div.system_submenu").css('display', 'block');
	$("#middle_left_cataloque_menu_items div.system_menu_item div.system_submenu:has(div.active_system_submenu_item)").css('display', 'block');
	
	$(".catalogue_group_photos, .catalogue_item, .articles_announce_group").mouseenter(function(){
		$(this).addClass('hover');
	});
	
	$(".catalogue_group_photos, .catalogue_item, .articles_announce_group").mouseleave(function(){
		$(this).removeClass('hover');
	});
    
/*$("div.system_menu_item, div.system_submenu_item").mouseenter(function() {
		//$("#"+$(this).attr("rel")).css("display","block");
        $("#"+$(this).attr("rel")).slideDown("slow");
		//var elem = document.getElementById($(this).attr("id"));
		var l = 0;
		var t = 0;

		if($(this).hasClass("system_menu_item"))
		{
			t += $(this).outerHeight();
		}

		if(!$(this).hasClass("system_menu_item"))
		{
			l = $(this).outerWidth();
			t = $(this).position().top;
		}

		$("#"+$(this).attr("rel")).css("left",l+"px");
		$("#"+$(this).attr("rel")).css("top",t+"px");
		//$("#"+$(this).attr("rel")).css("display","block");
        $("#"+$(this).attr("rel")).slideDown("slow");
	});

	$("div.system_menu_item, div.system_submenu_item").mouseleave(function() {
		if($(this).hasClass("system_submenu"))
            //$(this).css("display", "none");
            $(this).slideUp("slow");
		//$("#"+$(this).attr("rel")).css("display","none");
        $("#"+$(this).attr("rel")).slideUp("slow");
	});*/
}

function DecorateHR()
{
	$("hr").replaceWith("<div class='delimiter'></div>");
}

function NextSlides()
{
	$(".left_slidetabs").data("tabs").next();
	$(".right_slidetabs").data("tabs").next();

	if(g_next_slide++ == 2)
		g_next_slide = 0;

	var date = new Date();

	//console.log(g_next_slide);

	SetCookie(
		"konturline_slide",
		g_next_slide,
		date.getFullYear(),
		date.getMonth(),
		date.getDate(),
		23,
		59
		);

	setTimeout(NextSlides, 10000);
}

function NextParthner()
{
	var sc = $("#browsable").data('scrollable');

	if(sc.getSize() - 5 > sc.getIndex())
	{
		$(this).parent().children('.right_arrow').removeClass('disabled');
		sc.next();
		if(sc.getSize() - 5 > sc.getIndex())
			$(this).removeClass('disabled');
		else
			$(this).addClass('disabled');
	}
	else
	{
		//$(this).addClass('disabled');
		sc.seekTo(0, 300);
	}
}

function NextParthnerWithTimeout()
{
	NextParthner();
	setTimeout(NextParthnerWithTimeout, 5000);
}

function InitializeParthners()
{
	$(".scrollable").scrollable(/*{speed: 1000}*//*{circular: true}*/);

	$(".right_arrow").click( function()
	{
		NextParthner();
		return false;
	});
	$(".left_arrow").click( function()
	{
		var sc = $("#browsable").data('scrollable');

		if(sc.getIndex() > 0)
		{
			sc.prev();
			$(this).parent().children('.left_arrow').removeClass('disabled');
			if(sc.getIndex() == 0)
				$(this).addClass('disabled');
			else
				$(this).removeClass('disabled');

		}
		return false;
	});
	
	setTimeout(NextParthnerWithTimeout, 5000);
}

function SetCookie ( name, value, exp_y, exp_m, exp_d, exp_th, exp_td, path, domain, secure )
{
	var cookie_string = name + "=" + escape ( value );

	if ( exp_y )
	{
		var expires = new Date ( exp_y, exp_m, exp_d, exp_th, exp_td );
		cookie_string += "; expires=" + expires.toUTCString();
	}

	if ( path )
		cookie_string += "; path=" + escape ( path );

	if ( domain )
		cookie_string += "; domain=" + escape ( domain );

	if ( secure )
		cookie_string += "; secure";

	document.cookie = cookie_string;
}

function DeleteCookie ( cookie_name )
{
	var cookie_date = new Date ( );  // Текущая дата и время
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function GetCookie ( cookie_name )
{
	var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

	if ( results )
		return ( unescape ( results[2] ) );
	else
		return null;
}

$(document).ready( function()
{
	var cookie_next_slide = GetCookie("konturline_slide");
	if(cookie_next_slide !== null)
		g_next_slide = Number(cookie_next_slide);

	DecorateHeaderMenu();
	DecorateCatalogue();
	DecoratePhotos();
	DecorateTables();
	DecorateContentDocuments();
	DecorateHR();
	$("div.contacts_tabs_top_item a").click( function()
	{
		$("div.contacts_tabs_top_item").removeClass("active_contacts_tabs_top_item");
		$(this).parent().parent().addClass("active_contacts_tabs_top_item");
		var selector = $(this).attr("rel");
		$("div.tabs_item").removeClass("active_tabs_item");
		$(selector).addClass("active_tabs_item");
		return false;
	});
	/*$("#header_search #search_hint_right a").click(function(){
 	$("#header_search #header_search_input").attr("value", $(this).html());
 	return false;
 	});*/
	$(".left_slidetabs").tabs(".left_images > div", {
		effect: 'fade',
		fadeOutSpeed: "slow",
		rotate: true,
		clickable: false
	}).slideshow();

	$(".right_slidetabs").tabs(".right_images > div", {
		effect: 'fade',
		fadeOutSpeed: "slow",
		rotate: true,
		clickable: false
	}).slideshow();

	$(".left_images > div").unbind("click");
	$(".right_images > div").unbind("click");

	/*$("#header_left div.left_images > div").hide();
 	$("#header_right div.right_images > div").hide();

 	$("#left_image_"+g_next_slide).show();
 	$("#right_image_"+g_next_slide).show();*/

	/*$(".left_slidetabs").data("slideshow").play();
 	$(".right_slidetabs").data("slideshow").play();*/

	setTimeout(NextSlides, 10000);

	InitializeParthners();
	if(jQuery.browser.msie && (jQuery.browser.version.substr(0,1) == "6" || jQuery.browser.version.substr(0,1) == "7"))
	{
		$("div.additional_item div.content_photo").each( function()
		{
			$(this).css("width", ($(this).children("img").width() + 2) + "px");
		});
	}

	if(jQuery.browser.msie && jQuery.browser.version.substr(0,1) == "6")
	{
		$("#header_menu_items div.menu_item div.menu_text a").each( function()
		{
			$(this).css("width", $(this).width() + "px");
			$(this).css("display", "block");
		});
	}

	$("#header_icons_print a").click( function()
	{
		window.print();
		return false;
	});
	
	var v = $('#m_delivery_1').val();
	if(v == 0 || v == 1)
		$('#m_address_1_label .red_star').remove();
	else
		$('#m_address_1_label').html('Адрес<span class="red_star">*</span>:');
	
	$('#m_delivery_1').change(function(){
		var v = $(this).val();
		if(v == 0 || v == 1)
			$('#m_address_1_label .red_star').remove();
		else
			$('#m_address_1_label').html('Адрес<span class="red_star">*</span>:');
	});
});
