
/* ******************** START IPHONE/IPAD/IPOD ******************** */

// stop text enlargement when viewing on iPhone/iPad/iPod
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i))) {
	document.write('<style type="text/css">html { -webkit-text-size-adjust: none; }</style>');
}

/* ******************** END IPHONE/IPAD/IPOD ******************** */

/* ******************** START FIREFOX ******************** */

// prevents dotted border when clicking buttons in Firefox
if (navigator.userAgent.indexOf('Firefox')!=-1) {
	document.write('<style type="text/css">button::-moz-focus-inner { border: 0; }</style>');
}

/* ******************** END FIREFOX ******************** */

/* ******************** START DOCUMENT READY ******************** */

$(document).ready(function(){

        /** Global Navigation Features **/
        $(".topLevel li").click(function(){
            $(this).find('.dropDown').show();
        }).hover(
            function(){},
            function(){
                $(this).find(".dropDown").hide();
        });

        $(".topLevel li .mainLink").click(function(){
            $(this).parent().find('.dropDown').show();
            return false;
        })


	/***** START WHat is coupon pal lightbox videos *****/
	$("#rightColWhatIsVideos li.video a").fancybox({
		'titlePosition'         : 'inside',
		'padding'		: 20,
		'type'			: 'iframe',
                'autoScale'             : true
		
	});

       $("#rightColWhatIsVideos li.img a").fancybox({
		'titlePosition'         : 'inside',
                'autoScale'             : true,
                'centerOnScroll'        : false

	});

        $("a#fancybox").fancybox({
            
        });

        $("a.couponLink").fancybox({

        });
	
	$("#signInLink").fancybox({
		'titleShow'		: false,
		'autoDimensions'        : false,
                'height'		: 270,
                'width'			: 500
	});
	
	$(".share a").fancybox({
		'titleShow'		: false,
		'autoDimensions'        : false,
                'height'		: 550
	});

        $(".storeTipsLink a").fancybox({
		'titleShow'		: false
	});

        $(".storeInfo a").fancybox({
		'titleShow'		: false
	});
	
	$(".infoBtn").fancybox({
		'titleShow'		: true
	});
	
	$(".registerLink").fancybox({
		'titleShow'		: true,
		'autoDimensions'        : false,
		'height'		: 380,
		'width'			: 500
	});

         //for admin view transactions
         $("a.voteTextLink").fancybox({});


	
	$('#lightBoxContent').css('display','none');

	$("#rightColWhatIsVideos li a").click(function(){		
		$("#rightColWhatIsVideos li").removeClass("active");
		$(this).parent().addClass("active");
	});
	/***** END WHat is coupon pal lightbox videos *****/
	
	

        /* COUPON BOX HOVER EFFECTS */
        $(".couponBox").live('mouseenter', function(){
            $(this).addClass('hovered');
        }).live('mouseleave', function(){
            $(this).removeClass('hovered');
        }).live('click', function(e){
            if($(e.target).parent().attr('class')=="merchant-logo-link"){
                return true;
            }
            //open coupon dialogue
            var slug = $(this).attr('slug');
            var url = "http://"+window.location.hostname+"/coupons/view/"+slug;
            if($(this).attr('show_dialogue')=='1'){
                //redirect current page
                window.location.assign(url);
                open_coupon_dialogue('/coupons/dialogue/'+slug);
            } else {
                window.open(url);
            }
            return false;
        });

        /* PRODUCT EFFECTS */
        $(".TopDealsCode").click(function(){
            var slug = $(this).attr('slug');
            var dialogue_type = $(this).attr('dialogue_type');
            var product_slug = $(this).attr('product_slug');
            var url = "http://"+window.location.hostname+"/merchants/view_product/"+product_slug;

            if($(this).attr('show_dialogue')=='1'){
                //redirect current page
                window.location.assign(url);
                //open coupon dialogue
                open_coupon_dialogue(dialogue_type+slug);
            } else {
                window.open(url);
            }
        });

        /* BLOG CONTENT TOGGELING */    
        $("#popular-posts .toggle-handle").click(function(e){

            //find target blog
            $parent = $(this).closest('.post-preview');

            //toggle summary
            $parent.find('.post-summary').toggle();
            
            //toggle handle text
            $toggle_text = $parent.find('.toggle-text a');
            if($toggle_text.html()==$toggle_text.attr('closed')){
                $toggle_text.html($toggle_text.attr('open'));
            } else {
                $toggle_text.html($toggle_text.attr('closed'));
            }

            //animate post
            $parent.find('.post-full').toggle('slow');

            return false;

        });

        /* REVEAL HIDDEN COUPONS */
        $(".revealCoupons").click(function(){
            $(this).closest(".coupon-list").find(".moreCoupons").slideDown();
            $(this).hide();
            return false;
        });
        

        $(".compare").click(function(){

            var list_id = $(this).closest(".compare-targets").attr("id");

            var url_list = new Array();
            $("#"+list_id+" li input:checked").each(function(){
                url_list.push($(this).val());
            });

            windowPopEvent(url_list);

        });


});
/* ******************** END DOCUMENT READY ******************** */


function windowPopEvent(url_list){

    var window_count = url_list.length;
    var window_width = screen.width/2;
    var window_height = screen.height/2;

    var left = window_width/4;
    var top = window_height/4;
    for(var i in url_list){
        var new_window = window.open(url_list[i], "_blank", "width="+window_width+",height="+window_height+",scrollbars=1,resizable=1,top="+top+",left="+left);
        if(!new_window){
            window.focus();
            var alert_msg = "Oops, a pop-up blocker prevented a comparison window from opening.\n\nIf there's a warning bar: \n1. Click on the warning bar \n2. Select \"Always Allow Pop-ups from This Site\" OR, check toolbars and other software that blocks popups";
            window.alert(alert_msg);
            break;
        }
        top += 20;
        left += 20;
    }
}
