var loginForm;
var regForm;

(function($){
 $.fn.extend({
 
 	customStyle : function(options) {
	  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
	  return this.each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
			
	  });
	  }
	}
 });
})(jQuery);


$(document).ready(function(){
    $(".terms-scroller").scrollable({circular:false, speed:1000});
    $('#press-tab-main').bastabs();
    setBlock('.press_item', '<ul class="items-block press-best" />', 6);
    setBlock('.slider-tab-item-home-fin', '<ul class="items-block-home-fin" /> ', 6);
    
    $(".slider-tab-show-home-fin").scrollable({speed:1000, circular:false, next:".next-video-home-fin",prev:".prev-video-home-fin"});
    //$(".vot-gr-brd").scrollable({circular:false, speed:1000});
    $(".flashVideo").click(function(){
        $("#videoBoxA").attr("href",$(this).attr("rel"));
        flowplayer("videoBoxA", {src:base_url+"/flowplayer-3.2.7.swf", wmode:"transparent"},{clip:{autoPlay:true}});
        $.fn.colorbox({href:"#videoBox",'open':true, 'inline':true});
    });
    
    
	$("#coin-slider").tabs(".slider-block", {
       effect: 'fade',
       fadeOutSpeed: "slow",
       rotate: true
	}).slideshow({autoplay:true, interval:3000});	
	$('.tab-content-left').bastabs();
	setBlock('.slider-tab-item', '<ul class="items-block" />', 8);
   $(".slider-tab-show").scrollable({speed:1000});;
	$('.dining-list li:nth-child(3n)').addClass('last-blk'); 
	$("a[rel='advt-gallery']").colorbox({height:"700px",width:"700px"});
        $("a[rel='winners-gallery']").colorbox({height:"700px",width:"700px",onOpen:function(){_gaq.push(['_trackEvent', 'Winners Gallery', 'Pictures']);}});
        $(".loginbox").attr("href",base_url+"/show_login");
        $(".loginbox").colorbox({onComplete: function() {
            //$.fn.colorbox.resize(900,600);
           $.fn.colorbox.resize();
           loginForm    = $("#clogin_form").validate({
                                rules:{
                                    email:{
                                        required:true,
                                        email:true
                                    },
                                    password:"required"
                                },
                                messages : {
                                  email:{
                                      required:"Please enter your email address",
                                      email:"Please enter a valid email address"
                                  },
                                  password:"Please enter password"
                                },
                                errorContainer: $("#error_container"),
                                errorLabelContainer: $("ol", $("#error_container")),
                                wrapper: 'li',
                                meta: "validate"
                            });
            regForm     = $("#creg_form").validate({
                                rules:{
                                    name:"required",
                                    email:{
                                        required:true,
                                        email:true
                                    },
                                    password:{
                                        required:true,
                                        minlength:6
                                    },
                                    phone:{
                                        
                                        digits:true
                                    }
                                },
                                messages:
                                    {
                                        name:"Please Enter a Name",
                                        email:{
                                            required:"Please enter your email address",
                                            email:"Please enter a valid email address"
                                        },
                                        password:{
                                            required:"Please enter a password",
                                            minlength:"Password should be atleast 6 Chars long"
                                        },
                                        phone:{
                                            
                                            digits:"Please enter a valid phone number"
                                        }
                                    },
                                    errorContainer: $("#error_container"),
                                    errorLabelContainer: $("ol", $("#error_container")),
                                    wrapper: 'li',
                                    meta: "validate"
                                
            });
            $(".custom_sel").customStyle();
    }});
        
        $("#convert_mop").numeric();
        $("#convert_mop").keyup(function(){
            $.get(base_url+"/convert_currency",{amount:$("#convert_mop").val()},function(data){
                data = $.parseJSON(data);
                $("#convert_usd").val(data.USD.value);
                $("#convert_gbp").val(data.GBP.value);
                $("#convert_inr").val(data.INR.value);
            });
        });
        $("[id*='content_']").hide();
        $("#content_0").show();
        $("a.tab").click(function () {
			
			
			// switch all tabs off
			$(".tab.active").removeClass("active");
			
			// switch this tab on
			$(this).addClass("active");
			
			// slide all content up
			$(".content").slideUp();
                        $(".content-vt").slideUp();
			
			// slide this content up
			var content_show = $(this).attr("rel");
			$("#"+content_show).slideDown();
		  
		});
                
                
                $(".terms-scroller").scrollable({circular:false, speed:1000});
                $(".videos-list li").click(function(){
                    
                    var videoPath = $(this).attr("rel");
                    
                    //$f("#player").getClip(0).stop();
                    //$f("#player").getClip(0).update({ url:nURL });
                    mediaplayer.setClip({url: videoPath});
                    mediaplayer.play();
                    $(".videos-list li").removeClass("tab-select");
                    $(this).addClass("tab-select");
                });
                
                $("#login_button").live('click',function(){
                   
                    if(loginForm.form())
                        {
                             $.post(base_url+"/custom_login",$("#clogin_form").serialize(),function(data){
                                if(data==1)
                                    {
                                        document.location.href=document.location.href;
                                        _gaq.push(['_trackEvent', 'Zee Ciine', 'Logins']);
                                    }
                                else
                                showMsg(data,"error");
                            })
                        }
                     
                   
                });
                $("#reg_button").live('click',function(){
                   
                    if(regForm.form())
                        {
                            $.post(base_url+"/custom_register",$("#creg_form").serialize(),function(data){
                                if(data==1)
                                    {
                                        document.location.href=document.location.href;
                                        _gaq.push(['_trackEvent', 'ZEE Cine', 'Registrations']);
                                    }
                                else
                                showMsg(data,"error");
                            })
                        }
                     
                   
                });
               
              
     $("#countdown").countdown({until: tickettime, compact: true,format:"H", 
    layout: '<li>{h1000}</li><li>{h100}</li><li>{h10}</li><li>{h1}</li>'}); 

          
     $("#home-count-down ul").countdown({until: tickettime, compact: true,format:"H", 
    layout: '<li>{h1000}</li><li>{h100}</li><li>{h10}</li><li>{h1}</li>'});       
       var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
       $("#month").html(tickettime.getDate()+','+months[tickettime.getMonth()]+','+tickettime.getFullYear()); 


$('.fb-nav li a').hoverIntent(function () {
        if ($(this).attr("id")) {
            $('.fb-pop').fadeOut("fast").filter("#sub-block-" + $(this).attr("id")).fadeIn("normal");
        } else {
            $('.fb-pop').fadeOut("fast");
        }
    }, function () {});
    $('header').hoverIntent(function () {

    }, function () {
        $('.fb-pop').fadeOut("slow");
    });
});


function read_more()
{
    $("#content-desc").hide();
    $("#content-detail").show();
}
function read_less()
{
    $("#content-detail").hide();
    $("#content-desc").show();
    
}
function myVote(questionId)
{
    //alert(questionId);
    $("body").append("<div id=\"splash-loader\">"+
"<div class=\"loader-bg\"></div>"+
"<div class=\"loader\"></div>"+
"</div>");
    if($("#fb-container").length)
        {
             showMsg("You are not logged in","warning");
        }
    else if($("[name='fr'][rel='"+questionId+"']:checked").length)
        {
            var answer = $("[name='fr'][rel='"+questionId+"']:checked").val();
            $.get(base_url+"/cast_vote",{que:questionId,ans:answer},function(data){
                var obj = eval(data);
                if(typeof obj.type!="undefined")
                    {
                        //$("#message").removeClass().addClass(obj.type);
                        //$("#message").html(obj.message);
                        showMsg(obj.message,obj.type);
                        if(obj.type=="success" && obj.method=="fb")
                            {
                                 FB.ui(
                                       {
                                         method: 'feed',
                                         message: 'I have Just voted for .'+obj.section,
                                         link:base_url+"/voting",
                                         picture:base_theme+"images/others/zee-cine-logo.png",
                                         action_links: [
                                           {text: 'Quiz', href: 'http://www.facebook.com/ZeeCineAwards?sk=app_331769270184962'}
                                         ],
                                         user_prompt_message: 'Let Others know about it'
                                       },
                                       function(response) {
                                        
                                       }
                                     );
                                     var ques       = $("que_"+questionId).text();
                                     var answer     = $("[name='fr'][rel='"+questionId+"']:checked").val();
                                     var answ       = $("ans_"+questionId+"_"+answer).text();
                                     var movie      = $("ans_"+questionId+"_"+answer).text();
                                     _gaq.push(['_trackEvent', 'Zee Voting', ques, answ+"-"+movie]);
                            }
                    }
                    else
                        {
                            $("#message").removeClass().addClass("error");
                            $("#message").html("There is some problem. please try again latter.");
                        }
                
                //$("#message").slideDown();
                //setTimeout("$('#message').slideUp()",3000);
            });
        }
        else
            {
                //$("#message").removeClass().addClass("error");
                //$("#message").html("Please make a choice");
                //$("#message").slideDown();
                //setTimeout("$('#message').slideUp();",3000);
                showMsg("Please make a choice","error");
            }
    //alert($("[name='fr'][rel='"+questionId+"']:checked").val());
     setTimeout("$('#splash-loader').remove();",2000);
    
}

function loginbox()
{
    $.fn.colorbox({href:base_url+"/show_login",'open':true, 'inline':true});
}
function showMsg(msg,type)
{
    if(!($("#error-block").length))
        {
            $("body").append("<div id=\"error-block\"><div></div><span><a href=\"javascript:void(0)\" onclick=\"javascript:hideMsg();\"><img src=\""+base_theme+"images/ok-bt.gif\" alt=\"\" /></a></span></div>");
        }
        $("#error-block").show();
        $("#error-block div").html(msg);
        $("#error-block div").removeClass().addClass(type);
}
function hideMsg()
{
    $("#error-block").hide();
}
