var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

BrowserDetect.init();

var movieList = null; //new Array(["castrol.swf",5], ["banner_447_-_560_x_125.swf",5]);
var bannercount=0;
function initList(page, part){
	$.get("getbanners.php?page=" + page + "&part=" + part + "&ts=" + new Date().getTime(), null, bannersLoadedCallback);
}

function bannersLoadedCallback(responseText, status){
	banners = responseText.split("|");
	movieList = new Array();
	for(i=0; i< banners.length;i++){
		if( banners[i] != '' ){
			movieList.push([banners[i].split(",")[0], banners[i].split(",")[1]]);	
		}
	}
	if( movieList.length > 0 ){
		playBanner();	
	}
}

function drawBanner(name, target){
	var so = new SWFObject(name, "movingbanner", "240", "400", "8", "#336699");
   	so.addParam("quality", "high");
   	so.addParam("wmode", "transparent");
   	so.addParam("salign", "t");
   	so.write(target);
}

function playBanner(){
	drawBanner(movieList[bannercount][0], "flashcontent");
   	setTimeout ( playBanner, movieList[bannercount][1] * 1000 );
   	if( bannercount < movieList.length - 1){
   		bannercount++;	
   		drawBanner(movieList[bannercount][0], "flashcontentpreloader");
   	}
   	else{
   		bannercount = 0;	
   	}
}

function drawMainBanner(name)
	{
		AC_FL_RunContent(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
				'width', '100%',
				'height', '100%',
				'src', name,
				'quality', 'high',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'align', 'right',
				'play', 'true',
				'loop', 'true',
				'scale', 'showall',
				'wmode', 'transparent',
				'devicefont', 'false',
				'id', name,
				'bgcolor', '#ffffff',
				'name', name,
				'menu', 'true',
				'allowFullScreen', 'false',
				'allowScriptAccess','sameDomain',
				'movie', name,
				'salign', ''
				); 	
	}

$(function() {
    $("#menu").lavaLamp({
        fx: "backout",
        speed: 700,
        click: function(event, menuItem) {
        }
    });
});

function setMenuPosition(){
    if( BrowserDetect.browser == 'Firefox' ){
        var menuPointerObjPos = $('#menu_pointer').offset();
        $('#menu').css( 'top',menuPointerObjPos.top);
        $('#menu').css('left', menuPointerObjPos.left);
    }
}

$(window).bind('resize', function() {
    setMenuPosition();
});

$(document).ready(function() {
    setMenuPosition();
	$('.print_link').click(function(){
            window.location = '/print_image.php?url=' + $(this).attr('href');
            return false;
        });

	jQuery('#truckshop').hover(function(){ 
		$(this).attr('src', 'i/butt_truckshop_hover.jpg');
		$('#shopCaption').html('грузовых автозапчастей');
	}, function(){ 
		$(this).attr('src', 'i/butt_truckshop_norm.jpg');
		$('#shopCaption').html('выберите нужный раздел');
	});
	
  	$("#truckshop").mouseup(function(){ 
  		$(this).attr('src', 'i/butt_truckshop_hover.jpg');
  	}).mousedown(function(){ 
  		$(this).attr('src', 'i/butt_truckshop_on.jpg'); 
  	});
  	
  	jQuery('#navi_home').hover(	function(){ $(this).attr('src', 'i/home_red.gif'); },
  								function () { $(this).attr('src', 'i/home_grey.gif'); });
  	
	var bookmarkurl="http://www.forum-auto.ru" 
	var bookmarktitle="Forum-auto" 
				
    /*jQuery('#navi_home').click( function(){
    	if (document.all)
			window.external.AddFavorite(bookmarkurl,bookmarktitle)
		else if (window.sidebar) // firefox	
			window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
    });*/

	jQuery('#navi_search').hover(function(){ $(this).attr('src', 'i/En-red.gif'); },
  								function () { $(this).attr('src', 'i/En-grey.gif'); });
  								
  	jQuery('#navi_mail').hover(	function(){ $(this).attr('src', 'i/mail_red.gif'); },
  								function () { $(this).attr('src', 'i/mail_grey.gif'); });
  								
  	jQuery('#close_news').hover(function(){ $(this).attr('src', 'i/news_close_on.gif'); },
  								function () { $(this).attr('src', 'i/news_close.gif'); });

	jQuery('#close_shop').hover(function(){ $(this).attr('src', 'i/news_close_on.gif'); },
  								function () { $(this).attr('src', 'i/news_close.gif'); });
  								
  	jQuery('#next_news_pic').hover(	function(){ $(this).attr('src', 'i/news_forward_on.gif'); },
  								function () { $(this).attr('src', 'i/news_forward.gif'); });
  								
  	jQuery('#prev_news_pic').hover(	function(){ $(this).attr('src', 'i/news_back_on.gif'); },
  								function () { $(this).attr('src', 'i/news_back.gif'); });
  	
  	jQuery('#carshop').hover(	function(){ $(this).attr('src', 'i/butt_carshop_hover.jpg');$('#shopCaption').html('легковых автозапчастей'); },
  								function () { $(this).attr('src', 'i/butt_carshop_norm.jpg');$('#shopCaption').html('выберите нужный раздел'); });
  	$("#carshop").mouseup(function(){ $(this).attr('src', 'i/butt_carshop_hover.jpg');}).mousedown(function(){ $(this).attr('src', 'i/butt_carshop_on.jpg'); });
  	
  	$("#carshop").click(function(){
  		jQuery.blockUI({  
	        message: jQuery('#modalShop'),  
	        css: { 	cursor: 'default', width:'300', height:'200px', top:  (jQuery(window).height() - 300) /2 + 'px', left: (jQuery(window).width() - 200) /2 + 'px', border: '1px solid black' }
		});
  	});
  	
  	/*$("#truckshop").click(function(){
  		jQuery.blockUI({  
	        message: jQuery('#modalShop'),  
	        css: { 	cursor: 'default', width:'300', height:'200px', top:  (jQuery(window).height() - 300) /2 + 'px', left: (jQuery(window).width() - 200) /2 + 'px', border: '1px solid black' }
		});
  	});*/
  	
  	loading = '<img src="i/loading.gif" />';
  	current = null;
  	next = null;
  	prev = null;
  	type = null;
  	part = null;
  	url = null;
  	
  	$(".view_vacancy").click(function(){
  		jQuery.blockUI({  
	        message: jQuery('#modalNews'),  
	        css: { 	cursor: 'default', width:'558px', height:'400px', top:  (jQuery(window).height() - 400) /2 + 'px', left: (jQuery(window).width() - 558) /2 + 'px', border: '1px solid black' }
		});
		current = $(this).attr('count');
		loadItem($(this).attr('href'), current);
  	});
  	
  	$(".view_contact").click(function(){
  		jQuery.blockUI({  
	        message: jQuery('#modalNews'),  
	        css: { 	cursor: 'default', width:'558px', height:'354px', top:  (jQuery(window).height() - 354) /2 + 'px', left: (jQuery(window).width() - 558) /2 + 'px', border: '1px solid black' }
		});
		current = $(this).attr('count');
		loadItem($(this).attr('href'), current);
  	});
  
	jQuery('.view_news').click(function() { 
	    jQuery.blockUI({  
	        message: jQuery('#modalNews'),  
	        css: { 	cursor: 'default', width:'560px', height:'460px', top:  (jQuery(window).height() - 460) /2 + 'px', left: (jQuery(window).width() - 560) /2 + 'px', border: '1px solid black' }
		});
		
		current = $(this).attr('count');
		loadItem($(this).attr('href'), current);
	});
	
	function loadItem(href, id){
		var qs = new Querystring();
		$.post( href + id + "&action=get&part=" + qs.get('part', 'car') + "&ts=" + new Date().getTime(), "", getPosCallback);
	}
	
	function getPosCallback(responseText, textStatus, XMLHttpRequest) {
		//alert(responseText);
		tmp = responseText.split('#SEP#', 2);
		if( tmp.length == 2 ){
			next = tmp[0].split('|', 5)[0];
	    	prev = tmp[0].split('|', 5)[1];
	    	type = tmp[0].split('|', 5)[2];
	    	url = tmp[0].split('|', 5)[3];
	    	part = tmp[0].split('|', 5)[4];
	    	
	    	if( prev == 'na' ){
				$('#prev_news').css('display', 'none');
				$('#next_news').css('display', 'inline');
				if( type != null ){
					$('#next_news').attr('href',url + "?" + "type=" + type + "&part=" + part + "&id=" );
				}
			}
			if( next != 'na' && prev != 'na'){
				$('#prev_news').css('display', 'inline');
				$('#next_news').css('display', 'inline');
				if( type != null ){
					$('#next_news').attr('href',url + "?" + "type=" + type + "&part=" + part + "&id=" );
					$('#prev_news').attr('href',url + "?" + "type=" + type + "&part=" + part + "&id=" );
				}
			}
			if( next == 'na' ){
				$('#prev_news').css('display', 'inline');
				if( type != null ){
					$('#prev_news').attr('href',url + "?" + "type=" + type + "&part=" + part + "&id=" );
				}
				$('#next_news').css('display', 'none');
			}
			if( next == 'na' && prev == 'na'){
				$('#prev_news').css('display', 'none');
				$('#next_news').css('display', 'none');
			}
			
			$("#newsContents").html(tmp[1]);
		}
		else{
			$("#newsContents").html(tmp[0]);
		}
    }
	
	jQuery('#close_shop').click(function() {
		jQuery.unblockUI();
		return false;
	});
	
	jQuery('#close_news').click(function() {
		jQuery.unblockUI();
		return false;
	});
	
	jQuery('#next_news').click(function() {
		loadItem($(this).attr('href'), next);
		return false;
	});
	
	jQuery('#prev_news').click(function() {
		loadItem($(this).attr('href'), prev);
		return false;
	});
	
	$('#password_recovery').click(function(){
		if( $('#recovery_login').val() == '' ){
			alert('Введите логин');
			return false;
		}
		$('#butt_recovery').attr('value', 'recoverit!');
		$('#recover_form').submit();
	});
	
	$('#SubmitAuth').click(function(){
		if( $('#login').attr('value') == "" || $('#password').attr('value') == "" ){
			alert("Введите имя пользователя и пароль.");
			return false;
		}
		$('#butt_auth').attr('value', 'authmenow!');
		$('#authform').submit();
	});
	
	$('#SubmitLogout').click(function(){
		$('#butt_leave').attr('value', 'logoutmenow!');
		$('#authform').submit();
	});
});
