$(document).ready(function() {
   
    //$("#slideshow").slideshow();
    
    
    $("#gift_form input#confirm-button").attr('disabled', 'disabled').css({"opacity": "0.25", "cursor" : "default" });
    
    $('#gift_form input[name="product"]').change(function() {
        
        $("#gift_form input#confirm-button").removeAttr('disabled').css({"opacity": "1", "cursor" : "pointer" });
        
    });
    
    
    // this will be needed for rating
    b4content = $("#ratemodal").html();
    
    $('#slideshow').cycle({fx: 'fade'});
    
    $("#rate").to_static();
        
    // by default dont slide to content - this will be changed if going to url that have #
    slidetocontent = false;

    $('#starrating').raty({
        path :      "/images/2011/",
        start :     0,
        target :    "#star_rating",
        hintList:   ['Very poor', 'Poor', 'OK', 'Good', 'Very good']
    });


    $('.videomodal, a[rel="videomodal"]').live("click", function() {
            $(this).youtube();
            return false;
    });
    

    $("#rate").click(function() {
           
        var positiontop = $(window).scrollTop();
        var windowheight = $(window).height();
           
        boxposition = positiontop + (windowheight / 2);
           
        $("#ratemodal").css('top', boxposition).fadeIn(750);

        $("body").append("<div id='ratecloser'></div>");

        $("#ratecloser, #modalclosebox").click(function() {

            $("#ratemodal").fadeOut(500);
            $("#ratecloser").remove();

        });

    });
    
    
    $(".claimpopup").live("click", function () {
       
        var positiontop = $(window).scrollTop();
        var windowheight = $(window).height();
           
        boxposition = positiontop + (windowheight / 2);
        
        $("#claimmodal").css('top', boxposition).fadeIn(750).append('<span id="modalclosebox"></span>');

        $("body").append("<div id='ratecloser'></div>");

        $("#ratecloser, #modalclosebox").click(function() {

            $("#claimmodal").fadeOut(500);
            $("#ratecloser").remove();

        });
        
        return false;
        
    });

	
    // top nav
    $("nav.top a").mouseenter(function() {
        var id = $(this).attr('class');
        var sliderid = "#slider_" + id;
		
        if ($(sliderid).attr('class') != 'active') {
            $('#sliders div.active').removeClass('active').stop(true).animate({
                left: 320
            }, 350);
        }
        
        $("#box_welcome").stop(true, true).delay(500).fadeOut(500);
		
        $(sliderid).stop(true).addClass('active').animate({
            left: 0
        }, 650);
        
        
		
    });
	
    $(".hider").mouseenter(function() {
        $('#sliders div').removeClass('active').stop(true).animate({
            left: 320
        }, 350);
        
        $("#box_welcome").fadeIn(500);
        
    });
	
	
    // botom left nav
    $("nav.left a").each(function(index,dom) {
        var id = index + 1;
        var name = "#slider_nav" + id;
        var html = $(name).html();
		
        $(dom).after(html);
    });

    $("nav.left h2").remove();	
    $("nav.left div").slideUp(1);
	
    $("nav.left div a.button").append("&nbsp;>");
	
    $("nav.left a").basicAccordion({
        allowclose : false
    });
	
	
    // slider links + top nav links
    // - smooth page slide
    // - content fadeout
    // - ajax load
    // - content fadein
    $('#sliders a,nav.top a,a.ajax, #footerul a').live("click", function() {
		
        if ($("#content").attr('class') != $(this).attr('href').split(".")[0]) {
			
            var uri = $(this).attr('href');
            //window.location.hash = this.hash.slice(1);
			
            $('#content').removeClass().contentLoader({
                content  : uri + " #ajax", 
                addclass : $(this).attr('rel')
            });
        }
        else {
            var targetOffset = $('#main').offset().top - 20;
            $('html,body').animate({
                scrollTop: targetOffset
            }, 1000);
        }
		
        $("nav.left div.active").slideUp(500);
        $("nav.left a").removeClass('active');
		
        return false;
    });


    //quick fix - have to check why its not working with "live"

    $('#main_left a.button').click(function() {
		
        if ($("#content").attr('class') != this.hash.slice(1)) {

            var uri = $(this).attr('href');
            //window.location.hash = this.hash.slice(1);

            $('#content').removeClass().contentLoader({
                content  : uri +" #ajax", 
                addclass : $(this).attr('rel')
            });
        }
        else {
            var targetOffset = $('#main').offset().top - 20;
            $('html,body').animate({
                scrollTop: targetOffset
            }, 1000);
        }

        $("nav.left div.active").slideUp(500);
        $("nav.left a").removeClass('active');

        return false;
    });
        
    // ie8 hack
    $("nav.left a").each( function(index, dom) {
        $(dom).css('margin', '0 0 10px 0');
            
    });
    
    
    $("#accordion a").basicAccordion();
    
    
    // change rate button color
    var currentcol = $("#center h1").css("color");
    $("#rate").fadeOut(300, function() {
    if ($("#content").attr('class') != "update" && $("#content").attr('class') != "index update") {
        
            $.ajax({
                    url: "/site/get_class/index/"+ $("#content").attr("class") +"/" + $("#campaign_interval_id").val(),
                    success: function(txt) {
                        $("#poll_id").val(txt);
                    }
            });
        
            $(this).css({
                'background-color' : currentcol
            }).fadeIn(500);
        }
    });
    
    
    $("#footerhider").toggle(
        function(){
            $("#footerul").slideUp(300, function() {
                $("footer").height(50);
                $("#footerhider").removeClass().addClass("show");
            });
        },
        function(){
            $("#footerul").slideDown(300, function() {
                $("footer").height(226);
                $("#footerhider").removeClass().addClass("hide");
            });
        }
    );
    
    
    // page rating
    
    $("#ratingsubmit").live('click', function() {
       
       $.post("/site/campaign/base", {
           'poll_id'    : $("#poll_id").val(),
           'poll_score' : $("#starrating-score").val(),
           'poll_comment':$("#description").val()
           },
           function(data) {
               $("#ratemodal").html("<p style='text-align: center; padding-top: 60px; font-size: 18px;'>Thank you for rating this article.</p>").delay(1500).fadeOut(300, function() { 
                                                                                                                                                                        $(this).html(b4content);
                                                                                                                                                                        $('#starrating').raty({
                                                                                                                                                                            path :      "/images/2011/",
                                                                                                                                                                            start :     0,
                                                                                                                                                                            target :    "#star_rating",
                                                                                                                                                                            hintList:   ['Very poor', 'Poor', 'OK', 'Good', 'Very good']
                                                                                                                                                                        });
                                                                                                                                                                    
                                                                                                                                                                        $("#rate").fadeOut(500);
                                                                                                                                                                    
                                                                                                                                                                    });
               $("#ratecloser").remove();
           }
       );
       
    });
    
    
    
    //check if we are on mobile device - then force resize of #content.left
                    if ($("body").width() < 640) {
                        
                        $("#content .left").css("width", "100%");
                        $("#accordion").css("width", ($("#center h1").width() - 20) + "px" );
                        $("#accordion a").css({"height": "auto","padding-right": "20px"});
                        $("#accordion div a").css({"height": "auto","padding-right": "0"});
                        
                        $("#center.equal, #center p, #center h2").css({"width":"100%", "margin":"15px 0"});
                        
                        $("#ssavers").css({"background": "none", "padding": 0});
                        
                    }
                    
        
        
        
    // temp just for demo
//    $("#compentry").live("click", function() {
//           
//        $("#compform").slideUp(300);
//        $("#compdescr").slideUp(300);
//           
//        $("#thanks").slideDown(300);
//           
//        return false;
//           
//    });



      if ($("#content").attr('class') == 'yourclients') {
                 
                // broker clients page
                $('table.display').dataTable({
                                            "sPaginationType": "full_numbers",
                                            "aoColumns": [
                                                            null,
                                                            null,
                                                            { "asSorting": false }
                                                         ]
                                            });

                }

});
