/*	

	------------------------------------------------------------------------
	Ridepot Scripting
	------------------------------------------------------------------------
	Ridepot
	Typ:            JavaScript
	Encoding:       UTF-8
	Version:        0.1.1
	
*/
var Me_Ride = {
	
	_browser: {
		ie: /*@cc_on!@*/false
	},
	start: function() {
		
		this.htmlManipulation();
		this.ajaxRequests();
		this.inputsStyles();
		this.inputsLabel();
		this.inputsHelp();
		
		this.formsValid();
		this.addGoogleStats();
	},
	
	/* =Input styles
	---------------------------------------------------------------------------- */
	inputsStyles: function() {
	
	},
	/* =Input labels style
	---------------------------------------------------------------------------- */
	inputsLabel: function() {
		$('input.clear').focus(function(){
			this.value='';
		});
		$('input:text, input:password').focus(function(){
			var label_id = $(this).attr("id");
			$("label[for=" + label_id + "]").css({color:"#0c63c5",fontWeight:"bold"});
			$(this).css({color: "#0c63c5"});
		});
		$('input:text, input:password').blur(function(){
			var label_id = $(this).attr("id");
			$("label[for=" + label_id + "]").css({color:"#5f5f5f",fontWeight:"normal"});
			$(this).css({color: "#a2a2a2"});
		});
	},
	/* =Helpers
	---------------------------------------------------------------------------- */
	inputsHelp: function() {
		 $("form .input a").cluetip({
  			splitTitle: '|',
			cluetipClass: 'jtip',
			arrows: true,
			width:220, 
			height:120
		}); 
		var rand_no = Math.random();
		rand_no = rand_no * 15;
		rand_no = Math.ceil(rand_no);
		$("#cluetip-outer").css({ background:"#fff url(css/default/tooltip/tooltip-car-" + rand_no + ".png) center center no-repeat" });
	},
	/* =HTML manipulation
	---------------------------------------------------------------------------- */
	htmlManipulation: function() {
		$("#splash #slideshow").innerfade({ 
				speed: 'slow', 
				timeout: 10000, 
				type: 'sequence', 
				containerheight: '220px' 
		 });
		$("#top-container #slideshow").innerfade({ 
				speed: 'slow', 
				timeout: 10000, 
				type: 'sequence', 
				containerheight: '380px' 
		 });
		 $("#splash h5 a.openid").click(function() {
			$('#splash h5 a.openid').hide();
			$('#splash form.form').hide();
			$('#splash h5 a.form').css("display", "inline");
			$('#splash form.openid').show();  
		 	return false;
		 });
		 $('#splash h5 a.form').click(function() {
			$('#splash h5 a.form').hide();
			$('#splash form.openid').hide();
			$('#splash h5 a.openid').css("display", "inline");
			$('#splash form.form').show();
		 	return false;
		 });
		 
		 // fixed slider bug
		 // handler function binded to each button
		 $("#top-container .slider .close a").live('click', function()
		 {
		 	$("#top-container .inner").slideToggle("slow", function callback()
			{		
				if ( $(this).is(":hidden") ) 
				{
				    $("a#slider-show").show();
					$("a#slider-hide").hide();
					createCookie('bar','hidden',365)							
      			}
      			else
      			{
					$("a#slider-show").hide();
					$("a#slider-hide").show();      			
					var date = new Date();// ?
					createCookie('bar','visible',365)
      			}
			});
		 	return false;
		 });
         //

		 //navigation click
		 $('#navi ul li a').live('click', function()
		 {   
			var indexTab = $("#navi ul li a").index(this);
			$("#navi ul li a").removeClass("selected");	
			$(this).addClass("selected").fadeIn("slow");
			//alert( indexTab );
			$("#top-container .about div").css("display", "none");
			$("#top-container .about div:eq(" + indexTab + ")").fadeIn("slow");									  
			
			return false;
    	 });
    	 
    	 
         /*
		 var container = $('div.sliderGallery');
				 if (container.length!=0) {
				 	var ul = $('ul', container);
				 	var itemsWidth = ul.innerWidth() - container.outerWidth();
				 	$('.slider').slider({
				 		minValue: 0,
				 		maxValue: itemsWidth,
				 		handle: '.handle',
				 		stop: function(event, ui){
				 			ul.animate({
				 				'left': ui.value * -1
				 			}, 500);
				 		},
				 		slide: function(event, ui){
				 			ul.css('left', ui.value * -1);
				 		}
				 	});
		 		}
		  */
		  $(".layer a[href='#close']").click(function() {
                $('#overlay').hide('slow');
          }); 
					
		var navigation = $('#navigation')
		if (navigation.length!=0) 
		{
		  	$('#navigation').accordion({
		  		active: false,
		  		alwaysOpen: false,
		  		active: 'h6.selected',
		  		header: 'h6.head',
		  		autoHeight: false,
		  		showSpeed: 400,
		  		hideSpeed: 800
		  	});
		} 	


		var navigation = $('#backstage-menu')
		if (navigation.length!=0) 
		{
		  	$('#backstage-menu').accordion({
		  		active: false,
		  		alwaysOpen: false,
		  		active: 'h6.selected',
		  		header: 'h6.head',
		  		autoHeight: false,
		  		showSpeed: 400,
		  		hideSpeed: 800
		  	});
		} 
		
				/* =Profile Box drag & drop
		--------------------------------- */
		if ($("#profile #boxes").length!=0) {
			$("#profile #boxes").sortable({
				placeholder: "placeholder", 
  	  	revert: true,
				cursor: "move"
			});
				$('#profile #boxes').sortable('disable')
		}
		$("#edit-places a").click(function(){
			$('#boxes').sortable('enable'); 
			$('#alert-places').fadeIn('slow');
			return false;
		});
		
		$("a#save-places").click(function(){
			$('#boxes').sortable('disable');
			
			order = $('#boxes').sortable('toArray');
			$('#box_order_input').val(order.join(',')); 
			$('#alert-places').fadeOut('slow'); 
			$('#box_order_form').submit();
			return false;
		});
		
		$("a#cancel-places").click(function(){
			$('#boxes').sortable('disable'); 
			$('#alert-places').fadeOut('slow'); 			
			return false;
		});
		
		$("#profile #photos-slider a").click(function() {		 
			$("#profile #photos-area #slide").slideToggle("slow", function callback(){
				if ($(this).is(":hidden")) {
					$("#profile #photos-slider a.point-down").css("display", "block");
					$("#profile #photos-slider a.point-up").css("display", "none");
      	}else{
					$("#profile #photos-slider a.point-up").css("display", "block");
					$("#profile #photos-slider a.point-down").css("display", "none");
      	}
			});
		 	return false;
		}); 
		
		
		/* Gallery hover effects */
		
		$(".gallery-avatar a").hover(function() {
			$(this).next("em").animate({opacity: "show", top: "-75"}, "slow");
		}, function() {
			$(this).next("em").animate({opacity: "hide", top: "-85"}, "fast");
		});
		
		$("ul.friends-list li > a").hover(function() {
			$(this).next("em").animate({opacity: "show", top: "-75"}, "slow");
		}, function() {
			$(this).next("em").animate({opacity: "hide", top: "-85"}, "fast");
		});
		
		$("div.photos-area a").hover(function() {
			$(this).next("em").animate({opacity: "show", top: "-75"}, "slow");
		}, function() {
			$(this).next("em").animate({opacity: "hide", top: "-85"}, "fast");
		});
		
		
		/* =Box options list

		--------------------------------- */

		$(".listmenu img.arrow").click(function(){ 
		    $("span.head_menu").removeClass('active');
		    submenu = $(this).parent().parent().find("div.sub_menu");
		    if(submenu.css('display')=="block"){
			    (this).parent().removeClass("active"); 	
			    submenu.hide(); 		
			    $(this).attr('src','/images/icons/arrow_hover.png');									
			}else{
				$(this).parent().addClass("active"); 	
				submenu.fadeIn(); 		
				$(this).attr('src','/images/icons/arrow_select.png');	
			}

			$("div.sub_menu:visible").not(submenu).hide();
			$(".listmenu img.arrow").not(this).attr('src','/images/icons/arrow.png');				
		})

		.mouseover(function(){ $(this).attr('src','/images/icons/arrow_hover.png'); })
		.mouseout(function(){ 
			if($(this).parent().parent().find("div.sub_menu").css('display')!="block"){
				$(this).attr('src','/images/icons/arrow.png');
			}else{
				$(this).attr('src','/images/icons/arrow_select.png');
			}

		});

		$(".listmenu span.head_menu").mouseover(function(){ $(this).addClass('over')})
			.mouseout(function(){ $(this).removeClass('over') });
		$(".listmenu div.sub_menu").mouseover(function(){ $(this).fadeIn(); })
			.blur(function(){ 
			$(this).hide();
			$("span.head_menu").removeClass('active');
		});								

		$(document).click(function(event){ 		
			var target = $(event.target);
			if (target.parents(".listmenu").length == 0) {				
				$(".listmenu span.head_menu").removeClass('active');
				$(".listmenu div.sub_menu").hide();
				$(".listmenu img.arrow").attr('src','/images/icons/arrow.png');
			}

		});	
		/*= TOP SPEED ajax*/
		/*obliczanie szerokosci ul na podstawie liczby elementow li*/
		/*
		var count = 0;
		$('ul#speedWoman li').each(function(){count++;});
		var szerW;
		szerW = 140*count;
		$('ul#speedWoman').width(szerW);
		
		var count2 = 0;
		$('ul#speedMan li').each(function(){count2++;});
		var szerM;
		szerM = 140*count2;
		$('ul#speedMan').width(szerM);
		*/
		
		/*Ajaxowe przeladowanie zawartosci sliderow*/		
		//$('#speedWoman').load("today.html");
		
			$("#is_motor").change(function(event) {
				$("#brand").load("/main/brandList?is_motor=" + $("#is_motor").val());
				$("#topRated").load("/main/topRated?" + $("#vehicleFilter").serialize());
				//$('#model_id').find('option:first').attr('selected', 'selected').parent('select');
				$("#model_id").children().remove();
			});
		
			
		$('.type-list a').click(function(){
			$("#type").val($(this).attr('id'));
			$("#topRated").load("/frontend_dev.php/main/topRated?" + $("#vehicleFilter").serialize());
			return false;	
		});		
			
			
			
		
		$('#speedWoman').load('/main/topSpeed/type/today/gender/woman');
				
		$('.woman-tabs ul li a').click(function(){
			$('.woman-tabs ul li a').removeClass("selected");
			$(this).addClass("selected");						  
		
			var plik = $(this).attr('href')+'.html';
			b = plik.length;
			var toLoad = plik.slice(1,b);
				
			var param = $(this).attr('id').split('_');
			var gender = param[0];
			var type = param[1];
			
			
			$('#speedWoman').css({'display':'none'}).hide('fast',loadContent);				
			function loadContent() {
				//$('#speedWoman').load(toLoad,'',showNewContent())
				$('#speedWoman').load('/main/topSpeed/type/' + type + '/gender/' + gender, '', showNewContent());
				
				
			}		
			function showNewContent() {			
				$('#speedWoman').show();
			}	
			return false;		
		});
		/*man content*/
		$('#speedMan').load('/main/topSpeed/type/today/gender/man');
		$('.man-tabs ul li a').click(function(){
			$('.man-tabs ul li a').removeClass("selected");
			$(this).addClass("selected");						  
		
			var plik = $(this).attr('href')+'.html';
			b = plik.length;
			var toLoad = plik.slice(1,b);
				
			var param = $(this).attr('id').split('_');
			var gender = param[0];
			var type = param[1];
			
			
			$('#speedMan').css({'display':'none'}).hide('fast',loadContent);				
			function loadContent() {
				//$('#speedWoman').load(toLoad,'',showNewContent())
				$('#speedMan').load('/main/topSpeed/type/' + type + '/gender/' + gender, '', showNewContent());
				
				
			}		
			function showNewContent() {			
				$('#speedMan').show();
			}	
			return false;		
		});
		
		
		/*slider*/
		
		$('.sliderGallery').css("overflow","hidden");
		$('div.one').after('<div class="slider oneSlider"><div class="handle"></div></div>');
		$('div.two').after('<div class="slider twoSlider"><div class="handle"></div></div>');
		var containerOne = $('div.one');
		var containerTwo = $('div.two');
		var ulOne = $('ul', containerOne);
		var ulTwo = $('ul', containerTwo);
        var itemsWidthOne = ulOne.innerWidth() - containerOne.outerWidth();
		var itemsWidthTwo = ulTwo.innerWidth() - containerTwo.outerWidth();
		
		/* =Top Speed Slider One
		--------------------------------- */
        $('.oneSlider').slider({
            min: 0,
            max: itemsWidthOne,
            handle: '.handle',
            stop: function (event, ui) {
                ulOne.animate({'left' : ui.value * -1}, 500);
            },
            slide: function (event, ui) {
                ulOne.css('left', ui.value * -1);
            }
        });
		
		/* =Top Speed Slider Two
		--------------------------------- */
		$('.twoSlider').slider({
            min: 0,
            max: itemsWidthTwo,
            handle: '.handle',
            stop: function (event, ui) {
                ulTwo.animate({'left' : ui.value * -1}, 500);
            },
            slide: function (event, ui) {
                ulTwo.css('left', ui.value * -1);
            }
        });
		
		/*---------------------------------*/
		
		
				/* =My Garage menu accordion

		--------------------------------- */
		
				/* =Search Options
		--------------------------------- */
		$('li#toVehicle').click(function() {
			$('#by-vehicle').show();
			$('#by-body-style').hide();
			$('#by-profile-data').hide();		
			$('#by-personal-info').hide();
			$('ul#option-bar li').removeClass('selected');
			$(this).addClass('selected');	
		 	return false;
		});
		$('li#toBodyStyle').click(function() {
			$('#by-vehicle').hide();
			$('#by-body-style').show();
			$('#by-profile-data').hide();		
			$('#by-personal-info').hide();
			$('ul#option-bar li').removeClass('selected');			
			$(this).addClass('selected');	
		 	return false;
		});
		$('li#toProfileData').click(function() {
			$('#by-vehicle').hide();
			$('#by-body-style').hide();
			$('#by-profile-data').show();		
			$('#by-personal-info').hide();
			$('ul#option-bar li').removeClass('selected');			
			$(this).addClass('selected');	
		 	return false;
		});
		$('li#toPersonalInfo').click(function() {
			$('#by-vehicle').hide();
			$('#by-body-style').hide();
			$('#by-profile-data').hide();		
			$('#by-personal-info').show();
			$('ul#option-bar li').removeClass('selected');			
			$(this).addClass('selected');	
		 	return false;
		});

        /* --------------------- */		
		/* =top bar login fields */
		/* --------------------- */
		if( $('.topbar-input-background #login-email').attr('value') == '' )
		{
		   $('.topbar-input-background #login-label').html('login');
		}
		else
		{
		    $('.topbar-input-background #login-label').html('');
		}
		
	    if( $('.topbar-input-background #pass').attr('value') == '' )
		{
		   $('.topbar-input-background #pass-label').html('password');
		}
		else
		{
		    $('.topbar-input-background #pass-label').html('');
        }
		
		
		$('.topbar-input-background #login-email').focus( function() 
		{   
		    $('.topbar-input-background #login-label').html('');
		});
		$('.topbar-input-background #login-email').blur( function() 
		{   
		    if( $(this).attr('value') == '' )
		    {
		        $('.topbar-input-background #login-label').html('login');
		    }
		    if( $('.topbar-input-background #pass').attr('value') == '' )
		    {
		        $('.topbar-input-background #pass-label').html('password');
		    }
		});
		
		
		$('.topbar-input-background #pass').focus( function()
		{
		    $('.topbar-input-background #pass-label').html('');
		});
	    $('.topbar-input-background #pass').blur( function() 
		{   
		    if( $(this).attr('value') == '' )
		    {
		        $('.topbar-input-background #pass-label').html('password');
		    }
		});

		 
	},
	/* =Ajax Requests
	---------------------------------------------------------------------------- */
	ajaxRequests: function() {	
		
		$(".ajax-link").live("click", function(){
			send_url = $(this).attr('rel');
			
			$.ajax({
			  url: send_url,
			  type: "GET",
			  success: function(html){
			    $("#flash-content").html(html);
			    $("#overlay").show('slow');
			  }
			});
			
			/*$("#flash-content").load(send_url, {type: 'GET'}, function(){
				$("#overlay").show('slow');
			});*/
			
		 	return false;
    });
   
		
		/*$(".ajax-link").click(function() {	
			send_url = $(this).attr('rel');
			
			$.ajax({
			  url: send_url,
			  type: "GET",
			  success: function(html){
			    $("#flash-content").html(html);
			    $("#overlay").show('slow');
			  }
			});
			
			/*$("#flash-content").load(send_url, {type: 'GET'}, function(){
				$("#overlay").show('slow');
			});
			
		 	return false;
		}); 
		*	/
		 
		/*$(".menu-item").click(function() {		 
			url = $(this).attr('rel');
			
			$("#garage-subcontent").load(url, {}, function(){
				this.ajaxRequests();
			});
			$("#garage-subcontent-header").text($(this).text())	;
		 	return false;
		}); */
	},
	/* =Forms Validation
	---------------------------------------------------------------------------- */
	formsValid: function() {
		
	},
	
	
	/* =Google Analitics
	---------------------------------------------------------------------------- */
	addGoogleStats: function() {
		var GAID = 'UA-1521362-3';
		var head = document.getElementsByTagName('head')[0];
		var scr = document.createElement('script');
		scr.type = 'text/javascript';
		scr.src = (location.protocol == 'http:' ? 'http://www.' : 'https://ssl.') + 'google-analytics.com/'+'urchin.js';
		var once = 0;
			function runMe() { 
				if (scr.readyState == 'loading' || urchinTracker == undefined || once) { return; }
				clearInterval(timer);
				once++;
				_uacct = GAID;
				urchinTracker();
			}
		var timer = setInterval(runMe, 2000);
		scr['onload'] = runMe;
		scr['onreadystatechange'] = runMe;
		head.appendChild(scr); 
	}
}


function refreshRels() {
	//if(loop == undefined) loop = true;
	//console.log("loop " + loop)
	$(".menu-item").click(function() {		 
			send_url = $(this).attr('rel');
			$('#loading-indicator').show();
			$.ajax({
			  url: send_url,
			  type: "GET",
			  success: function(html){
			    $("#subcontent").html(html);
			    $('#loading-indicator').hide();
					refreshRels();
			  }
			});
		 	return false;
	}); 
	
		$(".ajax-link").live("click", function(){
			send_url = $(this).attr('rel');
			
			$.ajax({
			  url: send_url,
			  type: "GET",
			  success: function(html){
			    $("#flash-content").html(html);
			    $("#overlay").show('slow');
			  }
			});
			
			/*$("#flash-content").load(send_url, {type: 'GET'}, function(){
				$("#overlay").show('slow');
			});*/
			
		 	return false;
    });
	
}

function setupAjax(){
	$("#loading-indicator").bind("ajaxSend", function(){
	 $(this).show();
	}).bind("ajaxComplete", function(){
	 $(this).hide();
	});
}

function activateMenu(pos,id){
	if(pos==-1){
		$('#mygarage .left-column .content ul li ul').show()
		accordionHacked = true;
	}else{
		$('#navigation').accordion('activate',pos);
		$(id).addClass("selected");
	}
}

function activateBackstage(pos,id){
	$('#backstage-menu').accordion('activate',pos);
	$(id).addClass('selected');
}

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 checkBar(){
	x = readCookie('bar')
	if(x){
	 if(x == 'hidden'){
	 	$("#top-container .inner").hide();
		$("a#slider-show").show();
		$("a#slider-hide").hide();	 	
	 }
	}
}



$(document).ready(function(){
	Me_Ride.start(); 
	//refreshRels(); 
	setupAjax();

});

