$(document).ready(function(){
	sectionPhoto();


	$("#button_reset").click(function(){
		window.location = window.location.href;
	});

	$("a[rel=blank]").click(function(){
		var newWnd = window.open($(this).attr("href"),"sabmillerblank");
		newWnd.focus();
		return false;
	});	
});
// end of dom load functions

function initSubbrands() {
	$("ul.subbrand li img").mouseenter(function(){
		var li = $(this).parent("li");
		li.css({background: "transparent url(/i/arrow_up.gif) no-repeat 50% 95%"});
	});
	$("ul.subbrand li img").mouseleave(function(){
		var li = $(this).parent("li");
		li.css({background: "transparent"});
	});
}

/* section photo gallery */
function sectionPhoto() {
	var sectionPhoto = $("#sectionPhoto");
	var sectPhotoList = $("ul.sectPhotoList", sectionPhoto);
	if ($(sectionPhoto).length > 0 && $(sectPhotoList).length > 0) {
		$("a",sectPhotoList).click(function(){
			var img = $("#sectPhoto");
			var a = $(this);
			var href = $(a).attr("href");
			if (href != "#" && href != "") {
				ajaxLoader(a);
				var newimg = $('<img src="'+href+'" alt="" id="sectPhoto" />');
				$(newimg).css({display:'none'});
				$(newimg).insertBefore(img);
			if (window.opera) {
				$(newimg).css({display:'block'});
			}
				$(newimg).load(function(){
					$(img).hide(250,function(){
					        $(img).remove();
					});
				        $(this).show(250);
					$("#ajaxLoader").hide();
				});
			}
			$("a.current",sectPhotoList).removeClass("current");
			$(a).addClass("current");
			return false;
		});
	}
}

function ajaxLoader(parent) {
	var ajaxLoader;
	if ($("#ajaxLoader").length < 1) {
		ajaxLoader = $('<img src="/i/ajax-loader.gif" alt="load..." width="16" height="16" id="ajaxLoader" />');
		$(ajaxLoader).appendTo("#main");
	} else {
		ajaxLoader = $("#ajaxLoader");
	}
	$(ajaxLoader).show();
	var pos = $(parent).position();
	$(ajaxLoader).css({left: (pos.left + $(parent).width()/2 - 8)+"px", top: (pos.top + $(parent).height()/2 - 8)+"px"});
}

/* проверка формы подписки */
function all_checkbox(){
    var day = document.getElementById('subscriber_day');
    var week = document.getElementById('subscriber_week');
    var month = document.getElementById('subscriber_month');
    elements = new Array(day,week,month); /* массив для чекбоксов периодичности рассылки */
    var cat1 = document.getElementById('1');
    var cat2 = document.getElementById('2');
    var cat3 = document.getElementById('3');
    var cat4 = document.getElementById('4');
    category = new Array(cat1,cat2,cat3,cat4); /* массив для категорий подписки (кол-во можно менять при необходимости) */
    var count = 0;

    for(j=0; j<category.length; j++){
        if(category[j].type == 'checkbox'){
            if(category[j].checked){
                count = 1;
                break;
            } else if (category[1].checked) {
                count = 1;
                break;
            } else if (category[2].checked) {
                count = 1;
                break;
            } else if (category[3].checked) {
                count = 1;
                break;
            } else {
                alert('Не выбрана категория');
                return false;
            }
        }
    }

    for(i=0; i<elements.length; i++)
    {    
        if (elements[i].type == 'checkbox')
	{
            if (elements[i].checked && count>0)
            {	
                return true;
            } else if (elements[1].checked && count>0) {
                return true;
            } else if (elements[2].checked && count>0) {
                return true;
            } else {
                alert('Не выбрана периодичность');
                return false;
            }
	}
    }
}

function checkbox(){  
    var day = document.getElementById('subscriber_day');
    var week = document.getElementById('subscriber_week');
    var month = document.getElementById('subscriber_month');
    elements = new Array(day,week,month); /* массив для чекбоксов периодичности рассылки */
    
    if(document.getElementById('inputsubscriber_title').value=='') {
        alert('Не заполнено поле E-mail');
        return false;
    }
    
    for(i=0; i<elements.length; i++)
    {    
        if (elements[i].type == 'checkbox')
	{
            if (elements[i].checked && count>0)
            {	
                return true;
            } else if (elements[1].checked && count>0) {
                return true;
            } else if (elements[2].checked && count>0) {
                return true;
            } else {
                alert('Не выбрана периодичность');
                return false;
            }
	}
    }

    
}


function closePopup(id) {
	var a = $("a.closepop", id);
	$(a).unbind('click');
	$(a).click(function(){
		$(id).hide(100);
		return false;
	});

}


/* create steps navigation */
function initStepsPage(id,photo) {
//return false
	var steps = $(id);
	if ($(steps).length > 0) {
		var step = $('div.step',steps);
		var href = window.location.href;
		href = href.split("#");
		var currentStep = $(step[0]);
		var nav = $('ul#step_nav li',steps);
		var currentNav = $(nav[0]);

		$(step).addClass("hide");
		$(step).css({opacity:0});

		$(step).each(function(i) {
			if (href[1] != "" && $(this).attr("id") == href[1]) {
				var ind = $(this).index();
				currentStep = $(step[ind]);
				currentNav = $(nav[ind]);
				window.scrollTo(0,0);
				window.onload = function() {
					window.scrollTo(0,0);
				}
			}
		});
		$(currentNav).addClass("current");
		$(currentStep).removeClass("hide");
		$(currentStep).animate({opacity:1},200,function(){});


		$("#main").addClass("hide");
		$("#main").removeClass("hide");

		$("a",nav).click(function(){
			var a = $(this);
			var li = $(a).parent('li');
			$(currentNav).removeClass("current");
			currentNav = $(li);
			$(currentNav).addClass("current");
			$(currentStep).animate({opacity:0},200,function(){
				$(this).addClass("hide");
				currentStep = $($(a).attr("href"));
				$(currentStep).removeClass("hide");
				$(currentStep).animate({opacity:1},200,function(){});
			});
			if (photo == true) {
				var i = $(li).index();
				var photos = $("#sectionPhoto ul.sectPhotoList li");
				if (i+1 <= $(photos).length) {
					var pli = $(photos).get(i);
					$("a",pli).click();

				}
			}

				$("#main").addClass("hide");
				$("#main").removeClass("hide");
			return	false;
		});
	}
}



function initAgenciesList(id){
	var agencies = $(id);
	if ($(agencies).length > 0){
		$(agencies).addClass('hide');
		var par = $(agencies).parent();
		ajaxLoader($(par));
		$(par).height("200px");
		var h3 = $("li h3:first-child", agencies);
		$(h3).addClass('hide');
		var li = $("li", agencies);
		$(li).addClass('hide');

		var navBlock = $('<div class="agNav"></div>');
		var rel = $('<div class="rel"></div>').appendTo($(navBlock));
		var nav = $('<div class="rel" id="agenciesNavigation"><ul class="box-rounded"></ul></div>');
		$(nav).appendTo($(rel));
		var box = $('<div class="box-rounded"></div>').appendTo($(rel));
                var currentA = $('<a href="#">'+$(h3[0]).text()+'</a>');
                $(currentA).appendTo($(box));

		$(h3).each(function(i){
			var navLi = $('<li></li>').appendTo($("ul",nav));
			var navA = $('<a href="#">'+$(this).text()+'</a>').appendTo($(navLi));
			$(navA).click(function(){
		                $(currentA).text($(this).text());
		                $(nav).css({left: '-10000px'});
		                $(li).addClass('hide');
		                $(li[i]).removeClass('hide');
		                
				return false;
			});
		});
		$(agencies).removeClass('hide');
		$(li[0]).removeClass('hide');
		$(navBlock).insertBefore($(agencies))	
	
		$(nav).css({left: '-10000px', zIndex: '100'});
		$(currentA).click(function(){
			$(nav).css({left: '0'});
			return false;
		});
		$(par).height("auto");
		$("#ajaxLoader").hide();
	}
}

function createComboList(id) {
	var combolist = $(id);
	if ($(combolist).length > 0){
		var chooseText = 'Выберите регион';
		$(combolist).addClass('hide');
		var par = $(combolist).parent();
		ajaxLoader($(par));

		var navBlock = $('<div class="clNav"></div>');
		var rel = $('<div class="rel"></div>').appendTo($(navBlock));
		var nav = $('<div class="rel" id="comboNavigation"></div>');
		$(nav).appendTo($(rel));
		var box = $('<div class="box-rounded"></div>').appendTo($(rel));
		chooseText = ($("a.current",combolist).text() != '')? $("a.current",combolist).text() : chooseText;
                var currentA = $('<a href="#">'+chooseText+'</a>');
                $(currentA).appendTo($(box));

		$(combolist).removeClass('hide');
		$(navBlock).insertBefore($(combolist))	
                $(combolist).appendTo($(nav));
                $(combolist).addClass("box-rounded");
	
		$(nav).css({left: '-10000px', zIndex: '100'});
		$(currentA).click(function(){
			$(nav).css({left: '0'});
//			$("a",combolist).unbind('click', hideCombolist).bind('click', {nav: $(nav)}, hideCombolist);
			$(document).unbind('mousedown', hideCombolist).bind('mousedown', {nav: $(nav)}, hideCombolist);
			return false;
		});
		$("#ajaxLoader").hide();
	}
}
function hideCombolist(event,nav) {
	var target = event.target;
	$("a",event.data.nav).each(function(){
		if ($(target).attr("href") == $(this).attr("href") && !$(target).hasClass("current")) 
			window.location = $(target).attr("href");
	});
	$(event.data.nav).css({left: '-10000px'});
	$(document).unbind('mousedown', hideCombolist);
	return true;
}

var t;
function runFileTest(){
	if(typeof t == "number") clearTimeout(t);
	t = window.setTimeout(function() {
	var val = $("#resumefile").val();
	if (val != '') {
		$("#inpfile label em").remove();
		$('<em>Файл: <strong>'+val+'</strong></em>').appendTo("#inpfile label");
		$("#inpfile label").addClass("inv");
	} else {
		runFileTest();
	}
	},500);
}



function initMainSectPhotoArray(sectPhotoArray) {
	var sectPhoto = $("#sectPhoto");
	var rand = Math.floor(Math.random() * (sectPhotoArray.length));

	$(sectPhoto).attr("src",sectPhotoArray[rand]);
	$(sectPhoto).removeClass("hide");
	$("#mainPhoto").click(function() {
		rand++;
		if (rand >= sectPhotoArray.length) {
			rand = 0;
		}
		$(sectPhoto).attr("src",sectPhotoArray[rand]);
	});
}
