// JavaScript Document.
var actual = "";

function wishlist(wich) {
	$('#'+wich+'_wishlist').fadeIn();
	$('#wishlist_options').hide();
}

function buyNow(product) {
	actual = $('#options').html();
	$('#options').html( '<img src="/store/images/ajax-loader.gif" />' );
    $('#options').load( '/scart/?action=addProduct&product_id=' + product );
}

function joinGroup(idevent, product) {
	$joinbutton = $('#options_event_'+idevent).html();
	$mediabox_joinbutton = $('#options_event_mediabox_'+idevent).html();
	$('#options_event_'+idevent).html('<img src="/store/images/ajax-loader.gif"/>');
	$('#options_event_mediabox_'+idevent).html('<img src="/store/images/ajax-loader.gif"/>');
	$.ajax({
		type:"GET",
		url:"/calendar/check_commit_permission.php",
		data:"event_id="+idevent,
		success:function(functionToRun){
			eval(functionToRun);
			$('#options_event_mediabox_'+idevent).html($mediabox_joinbutton);
			$('#options_event_'+idevent).html($joinbutton);
		}
	});
}

function openMaxCommitsErrorModal(event)
{
	$.fancybox({
		'width'         : '45%',
		'height'        : '32%',
		'autoScale'     : false,
		'autoDimensions': false,
		'href'			: '/calendar/max_commits_error.php?event=' + event,
		'type'   		: 'iframe',
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
	});
}

function openScoreRateErrorModal(group_id, type)
{
	$.fancybox({
		'width'         : '45%',
		'height'        : '55%',
		'autoScale'     : false,
		'autoDimensions': false,
		'href'			: '/calendar/subscription_score_error.php?id=' + group_id + '&type=' + type,
		'type'   		: 'iframe',
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
	});
}

function openGenericModal(url)
{
	$.fancybox({
		'width'         : '55%',
		'autoScale'     : true,
		'autoDimensions': true,
		'href'			: url,
		'type'   		: 'iframe',
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
	});
}

function justSubscribe(idevent,product)
{
	$.fancybox({
		'width'         : '55%',
		'autoScale'     : false,
		'autoDimensions': false,
		'href'			: "/calendar/controller/confirm_participation.php?event="+idevent+"&committed=0",
		'type'   		: 'iframe',
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
	});
}

function subscribeGroup(obj, idevent) {
	if (obj.checked) {
		jQuery.get("/calendar/controller/confirm_participation.php", {event:idevent, committed:0});
	} else {
		jQuery.get("/calendar/controller/cancel", {id:idevent});
	}
}

function editGroup(idevent) {
	actual = $('#options').html();
	$('#options_event_'+idevent).html('<img src="/store/images/ajax-loader.gif" />');
	$('#options_event_'+idevent).load("/calendar/controller/participate?event="+idevent);
}

function cancelGroup(idevent, product) {
	actual = $('#options').html();
	if (confirm('Are you sure you want to cancel your group commitment?')) {
		$('#options_event_'+idevent).html('<img src="/store/images/ajax-loader.gif" />');
		jQuery.get("/calendar/controller/cancel", {id:idevent}, function (result) {
			window.open('/store/product/'+product, '_self');
		});
	}
}

function back() {
	$('#options').html(actual);
}

function closePopup() {
	$("#top_block").slideUp();
	$('#blank').load('/store/no_popup.php');
}

function showPopup() {
	$("#top_block").slideDown();
}

function priceComparison(id)
{
    var div = $('#price_comparison');
    if (!div.data('busy')) {
	    div.data('busy', true);
	    div.load('/store/price_comparison.php?id=' + id, function(){
	    	div.data('busy', false);
	    });
    }
}


function loadBuyNow(id) {
    var div = $('#buy_now');
    if (!div.data('busy')) {
	    div.data('busy', true);
	    div.load('/store/buy_now_price.php?id=' + id, function(){
	    	div.data('busy', false);
	    });
    }
}


function resize_image(img, width, height) {

	if(navigator.userAgent.toLowerCase().indexOf("msie") == -1) return;
	if(img.width>width && img.width > img.height) {
		img.style.width= width+'px';
	} else if(img.height>height) {
		img.style.height = height+'px';
	}
}

function zoom_image(src) {
	window.open('http://'+location.host+'/store/zoom.php?img='+src, 'zoom_image', 'width=600,height=600,status=no,menubar=no,resizable=no,scrollbars=no,titlebar=no');
}

function update_image(urlthumb, url) {
	$('#product_image').attr("src", urlthumb);	
	$('#product_image').click(function(){zoom_image(url)});
	$('#product_image_zoom').click(function(){zoom_image(url)});
}

function order_invoice(id) {
	window.open('/scart?action=getOrderInfo&id='+id+'&show_groups=1', 'invoice'+id, 'width=600,height=600,status=no,menubar=no,resizable=no,scrollbars=yes,titlebar=no');
}

function details_purchase(id) {
	window.open('/calendar/purchase_details?id='+id, 'details'+id, 'width=400,height=400,status=no,menubar=no,resizable=no,scrollbars=yes,titlebar=no');
}

function do_placeholder() {                    

	if ($.browser.safari) {
	   	// very nice
		return;
	}

	$("input[placeholder]").each(function(el) {

		// don't do the input again
	    if ($(this).attr('placeholder_done')) {
			$(this).attr('placeholder_done', true);
		} else {
			// initial value
			if ($(this).val() == "") {
				$(this).val($(this).attr("placeholder")).addClass('placeholder');
			}             

           	// handle focus event (hide placeholder)
			$(this).focus( function() {
				if ($(this).val() == $(this).attr("placeholder")) {
					$(this).removeClass('placeholder').val("");   
				}
			});

			// handle blur event (show placeholder if empty value)
			$(this).blur( function() {
				if ($(this).val() == "") {
					$(this).addClass('placeholder').val($(this).attr("placeholder"));
				}
			});			
		}
	});

	// don't handle this form again
    if ($(this.form).attr('placeholder_done')) {
		$(this.form).attr('placeholder_done', true);
	} else {
		$(this.form).submit( function() {
			$("input[placeholder]").each(function(el) {
				if ($(this).val() == $(this).attr("placeholder")) {
					$(this).val("");
				}
			}
		)}); 
	}
}

function updateLocationSelects(Field){
	
}

function emailMktSubscribe(Email,Country,City){
	$("#subscription_ajax_loader").show();
	$.ajax({
		type:"GET",
		url:"/store/save_email_marketing.php",
		data:"email="+Email+"&country="+Country+"&city="+City,
		success:function(Data){
			if(Data == "INVALID_EMAIL_ADDRESS"){
				alert("E-mail does not appear to be a valid e-mail address, should be format of user@domain.com");
				$("#subscription_ajax_loader").hide();
			} else {
				$('#email_mkt_subscription').slideUp();
				document.cookie='email_mkt_subscription=disabled; expires=; path=/;';
			}
		}
	});
}

function updateClickCount(link) {
	$.ajax({
		type:'GET',
		url:'/store/register_click_ajax.php',
		data:'uri='+location.pathname,
		success: function(Result){
			if(Result == "1"){
				location.href = link;
			} else {
				location.href= link;
			}
		}
	});
}

function openRankModal(group_id)
{
	$.fancybox({
		'width'         : '55%',
		'autoScale'     : false,
		'autoDimensions': false,
		'href'			: '/calendar/rank_of_group.php?id=' + group_id,
		'type'   		: 'iframe',
		'transitionIn'	: 'none',
		'transitionOut'	: 'none'
	});
}

function showFacebookDiv(divNumber) {
	$(".facebook_buddies").hide('slow');
	$("#div_number_" + divNumber).show('slow');
}

//start on page load
$(do_placeholder);
		
$(document).ready(function(){
	$("#refine_boxes_tabs").tabs();
	
});

function addCommit(eventId){  
    $.ajax({
        type: "GET",
        url: "/calendar/check_commit_permission.php",
        data: "event_id="+eventId,
        success: function(response){
            eval(response);
        }
    })
}

function loadMoreDetails(){
   $('#optdetails').addClass('.disable-option'); 
   $('#optresume').removeClass('.disable-option'); 
   $('#title').html('Score Detailed');
   $('#score-content').fadeOut('fast', function(){ 
        $('#score-details-content').hide().html('');
        $('#score-loader').html('<span>Loading content</span><br /><img src="http://'+location.host+'/store/images/ajax-loader.gif"/>');
        $('#score-loader').show();
        $('#score-details-content').load('/calendar/score.php?details=ok', function(){
            $('.resume-history tr:nth-child(odd)').addClass('odd');
            $('.resume-history tr:nth-child(even)').css('background-color','#DCDCDC');
            $('#score-loader').hide();
            $('#score-details-content').fadeIn('slow');
        });
   });
}

function loadResume(){
    $('#optresume').addClass('.disable-option'); 
    $('#optdetails').removeClass('.disable-option'); 
    $('#score-details-content').fadeOut('fast',function(){
        $("#title").html('Score Summary');
        $('#score-content').hide().html('');
        $('#score-loader').show();
        $('#score-loader').html('<span>Loading content</span><br /><img src="http://'+location.host+'/store/images/ajax-loader.gif"/>');
        $('#score-content').load('/calendar/score.php?resume=ok', function(){
            $(".info-us").tooltip({ effect: "slide", tipclass:"tooltip-us"});
            $('.resume-history tr:nth-child(odd)').removeClass('odd');
            $('#score-loader').hide();
            $('#score-content').fadeIn('slow');
        });
    });
}

function scoreLoadPage(page,order){
    var query = '/calendar/score.php?details=ok&page='+page+'&order='+order;
    $('#score-details-content').fadeOut('fast', function(){
        $('#score-loader').show();
        $('#score-loader').html('<span>Loading content</span><br /><img src="http://'+location.host+'/store/images/ajax-loader.gif"/>');
        $.ajax({
            type: "GET",
            url: "/calendar/score.php",
            data: "details=ok&page="+page+"&order="+order,
            success: function(data){
                $('#score-details-content').html(data);
                $('#score-loader').hide();
                $('#score-details-content').fadeIn('slow');
            }
        })
   });
}

//google +1 button handle
function googlehandling(event){
    if(event.state == 'on'){
        $.post('/calendar/buttonhandling', {type: 'plus'});
    }
}

function moreInfo(id){
    $.fancybox({
        'width'         : '50%',
        'height'        : '50%',
        'autoScale'     : false,
        'autoDimensions': false,
        'href'          : '/calendar/moreinfo.php?id='+id,
        'type'          : 'iframe',
        'transitionIn'  : 'none',
        'transitionOut' : 'none'
    });
}   
