// JavaScript Document
//var slider = '';
var cur_rollover = '';
var cur_e = '';
var cur_t = '';


$(document).ready(function()
{
	var left_h = $('#product_result_left').height() - 15;
	var right_h = $('#right_box .bg_box').height();
	
	if(right_h < left_h) {
		$('#right_box .bg_box').css('height', left_h);
	}
	
	$('#btn_add_to_selector').click(function(){
		$('#btn_add_to_selector').css("display","none");
		$('#btn_add_to_selector_grey').css("display","block");
	});
	
	
	if($("#product_sliding_bar").html() != undefined){
		sliding_bar_apply_hover();
	}
	
	if($("#collection_items").html() != undefined){
		collection_items_apply_hover();
	}
});


// slider
$(function() {
	var s = parseFloat($('#playhead').val());
	
    $(".jCarouselLite").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
		start: s,
		visible: 6,
		scroll: 3,
		speed: 1000,
		circular: false,
		afterEnd: function(a) {
			$('#playhead').val($(a[0]).children('p.pos').html());
		}
    });
});


// sliding bar
window.onload = function () {
	var container = $('div.sliderGallery');
	var ul = $('ul', container);
	
	var maxValue = $('#sliding_bar ul li').length - 6;
	
//	slider = $('.slider', container).slider({
	$('.slider', container).slider({
		min: 0,
		max: maxValue,
		stepping:1,
		handle: '.handle',
		slide: function (event, ui) {
			ul.css('left', (ui.value * -88)+'px');
//			$('.next').trigger('click');
		}
	});
};


// open selected result
function viewResult(url) {
	$('#form_playhead').attr('action', url);
	$('#form_playhead').submit();
}


// sliding bar popups
function sliding_bar_apply_hover(){
	$("#product_sliding_bar ul li.popup_require").bind("mouseover", function(e){
		cur_rollover = $(this).children('p').html();
		cur_e = e;
		
		cur_t = setTimeout('show_ajax_div($("#product_popup"), cur_rollover, cur_e)', 500);
	});
	$("#product_sliding_bar ul li.popup_require").mousemove(function(e){
		if($("#product_popup").html() != '') {
			move_ajax_div($("#product_popup"), e);
		}
	});
	$("#product_sliding_bar ul li.popup_require").mouseout(function(e){
		clearTimeout(cur_t);
		hide_div($("#product_popup"));
	});
}

function show_ajax_div(popup, link, pos){
	popup.html('<img src="/images/loading.gif" width="16" height="16" alt="loading..." /><iframe border="0"></iframe>');
	
	var w = $(window).width();
	
	if(pos.pageX >= w/2+30) {
		popup.css('left', pos.pageX-370+'px');
		popup.css('top', pos.pageY+25+'px');
	} else {
		popup.css('left', pos.pageX+20+'px');
		popup.css('top', pos.pageY+25+'px');
	}
	
	$.ajax({
		type:'POST',
		url: link,
		data:'type=product',
		success:function(html){
			popup.html(html);
		}
	});
}

function move_ajax_div(popup, pos){
	var w = $(window).width();
	
	if(pos.pageX >= w/2+30) {
		popup.css('left', pos.pageX-370+'px');
		popup.css('top', pos.pageY+25+'px');
	} else {
		popup.css('left', pos.pageX+20+'px');
		popup.css('top', pos.pageY+25+'px');
	}
}

function hide_div(popup){
	popup.html('');
	popup.css('left', '-1000px');
}


// collection items popups
function collection_items_apply_hover(){
	$("#collection_items li").bind("mouseenter", function(e){
		cur_rollover = $(this).children('p').html();
		cur_e = e;
		
		cur_t = setTimeout('collection_show_ajax_div($("#product_popup"), cur_rollover, cur_e)', 500);
	});
	$("#collection_items li").mousemove(function(e){
		if($("#product_popup").html() != '') {
			collection_move_ajax_div($("#product_popup"), e);
		}
	});
	$("#collection_items li").mouseout(function(e){
		clearTimeout(cur_t);
		hide_div($("#product_popup"));
	});
}

function collection_show_ajax_div(popup, link, pos){
	popup.html('<img src="/images/loading.gif" width="16" height="16" alt="loading..." /><iframe border="0"></iframe>');
	
	popup.css('left', pos.pageX-370+'px');
	popup.css('top', pos.pageY-203+'px');
	
	$.ajax({
		type:'POST',
		url: link,
		data:'type=product',
		success:function(html){
			popup.html(html);
		}
	});
}

function collection_move_ajax_div(popup, pos){
	popup.css('left', pos.pageX-370+'px');
	popup.css('top', pos.pageY-203+'px');
}


// add to selector
function addToSelector(link) {
	$.ajax({
		type:'POST',
		url: link,
		data:'',
		success:function(html){
			$('#shortlist_wrap').html(html);
		}
	});
}
