﻿// JavaScript Document

var entry_count = 0;
var active_entry = 0;
var $brands;
var brand_width_arr = Array();
var brands_inner_width = 0;
var num_brands = 0;
var current_brand = 0;
var current_pos = 0;
var $current_main_menu;
var $default_main_menu;

var $slideshow_photos, $slideshow_nav, $slideshow_placeholders;

// For slideshow
var t = null;
var t2 = null;
var t3 = null;
var t4 = null;

$(document).ready(function () {
	// SLIDESHOW

	// Collects all the photos in the main slideshow
	$slideshow_photos = $("#slideshow_images img.slideshow_img");
	$slideshow_placeholders = $("#slideshow_images img.placeholder");
	$default_main_menu = $('#nav ul.dropdown li a.active');
	// The number of images in the slideshow
	entry_count = $slideshow_photos.length;
	$slideshow_nav = $("#slideshow_nav ul li");
	// Create slideshow navigation
	$slideshow_photos.hide();
	$slideshow_nav.children('a').click(function (e) {
		if (t2) clearTimeout(t2);

		active_entry = parseInt($(this).attr('rel'));
		$slideshow_nav.removeClass('current');
		$(this).parent('li').addClass('current');
		$slideshow_placeholders.eq(0).attr('src', $slideshow_photos.eq(active_entry).attr('src'));
		next_entry = active_entry < (entry_count - 1) ? active_entry + 1 : 0;
		$slideshow_placeholders.eq(1).attr('src', $slideshow_photos.eq(next_entry).attr('src'));
		clearTimeout(t);

		t2 = setTimeout(restart_slideshow, 6000);

		e.preventDefault();

	});

	/*$slideshow_nav.children('a').mouseout(function(){
	t = setInterval(advance_slideshow, 5000);
	});*/



	// Automatically advances the main slideshow after an arbitrary period of time
	function advance_slideshow() {

		if (t2) clearTimeout(t2);

		next_entry = active_entry < (entry_count - 1) ? active_entry + 1 : 0;
		$slideshow_placeholders.eq(0).fadeOut(500, function () {
			$slideshow_nav.removeClass('current');
			$slideshow_nav.eq(next_entry).addClass('current');
			$slideshow_placeholders.eq(0).attr('src', $slideshow_photos.eq(next_entry).attr('src'));
			third_entry = next_entry < (entry_count - 1) ? next_entry + 1 : 0;
			$slideshow_placeholders.eq(1).attr('src', $slideshow_photos.eq(third_entry).attr('src'));
			active_entry = next_entry;
			$slideshow_placeholders.eq(0).show();

		});

		return false;

	}

	function restart_slideshow() {

		t = setInterval(advance_slideshow, 4000);

	}

	//Added by Zeon
	$slideshow_nav.children('a').eq(0).click();
	//---

	t = setInterval(advance_slideshow, 4000);

	/* Brand Gallery */

	$brands = $('#brands_inner div');

	$brands.each(function (index) {

		var img_width = $(this).width();
		brand_width_arr[index] = img_width;
		brands_inner_width += img_width;
		num_brands++;

	});

	$('#brands_inner').width(brands_inner_width);

	$('.btn_back').click(function (e) {

		if (current_brand > 0) {

			var new_pos = current_pos + brand_width_arr[current_brand - 1];
			current_pos = new_pos;
			$('#brands_inner').animate({ left: new_pos }, 500, function () { });
			current_brand--;

		}

		e.preventDefault();

	});

	$('.btn_next').click(function (e) {

		if (current_brand < (num_brands - 1)) {

			var new_pos = current_pos - brand_width_arr[current_brand];
			//console.log(new_pos);
			$('#debug').val(new_pos);
			current_pos = new_pos;
			$('#brands_inner').animate({ left: new_pos }, 500, function () { });
			current_brand++;

		}

		e.preventDefault();

	});



	$('#btn_language').mouseenter(function (e) {

		$(this).addClass('active');
		$('#languages ul').show();

	});

	$('#btn_language').mouseleave(function (e) {

		t3 = setTimeout(function () {

			$(this).removeClass('active');
			//console.log($(this).attr('class'));
			$('#languages ul').hide();

		}, 100);

	});

	$('#languages ul').mouseenter(function (e) {

		$('#btn_language').addClass('active');
		clearTimeout(t3);

	});

	$('#languages ul').mouseleave(function (e) {

		$('#btn_language').removeClass('active');
		$(this).hide();

	});

	/* Drop-down menus */

	$('#nav ul li a').mouseenter(function (e) {

		$('#nav ul.dropdown li a').removeClass('active');
		console.log($default_main_menu);

		if ($(this).hasClass('dir')) {

			$(this).addClass('active');
			$current_main_menu = $(this);

		}

	});

	$('#nav ul li a').mouseleave(function (e) {

		if ($(this).hasClass('dir')) {

			$(this).removeClass('active');

		}

		$default_main_menu.addClass('active');

	});

	$('#nav ul.dropdown ul').mouseenter(function (e) {

		//clearTimeout(t4);
		$current_main_menu.addClass('active');

	});

	$('#nav ul.dropdown ul').mouseleave(function (e) {

		$current_main_menu.removeClass('active');
		$default_main_menu.addClass('active');

	});

	$('#searchfield #search').focus(function (e) {

		if ($(this).val() == 'search') {

			$(this).val('');

		}

	});

	$('#searchfield #search').blur(function (e) {

		if ($(this).val() == '') {

			$(this).val('search');

		}

	});

});


