﻿    /// <reference path="jquery-1.3.2-vsdoc.js" />
/// <reference path="Common.js" />
/// <reference name="MicrosoftAjax.js" />
/// <reference path="../Services/ProductService.asmx" />

var _clicks;
var _boxTracking = false;
var _boxX;
var _boxY;
var _boxX2;
var _boxY2;

function ExitModalPopup()
{
    var _happyPeople = $("#happypeople");
   
    $("#exitmodal").css("top", _happyPeople.offset().top + _happyPeople.height()).css("width", 406);
    
    $.get("/Tools/GetSalesCode.aspx", { rand: Math.random() }, function(data) {
        $("#callincode").html(data);
    });
    
    $("closeexitmodalpopup").css("cursor", "hand");
    $("#closeexitmodalpopup").css("margin-left", 343);
    
    $("#exitmodal").slideDown("slow");
    
    $("#closeexitmodalpopup").click(function(){
        $("#exitmodal").slideUp("slow");
    });
    
     $("#exitcontinuebrowsing").click(function(){
        $("#exitmodal").slideUp("slow");
    });
    
     $("#exitcontinuebrowsing").click(function(){
        $("#exitnotlookingfor").slideUp("slow");
    });
}

function CloseModal(){

     $("#exitmodal").slideUp("slow");
}
$(function() {

    if ($("#homepagepopup").length > 0){
        setTimeout(ExitModalPopup, 15000);
    }
    
    $().click(function(e) {
        trackclick(e, $('#canvas'));
    });

    jQuery.preloadImages = function() {
      for(var i = 0; i<arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
      }
    } 
    
    $('#carto-viewer-window').dialog({
        autoOpen: false,
        height: 750,
        width: 675,
        draggable: true,
        resizable: false,
        buttons: {
            "Close": function() {
                $(this).dialog("close");
            }
        },
        position: ['center', 'top']
    });

    $('.carto-viewer-link').click(function() {
        $('#carto-viewer-window').dialog('open');
        return false;
    });
    
    $('#free-shipping-window').dialog({
        autoOpen: false,
        width: 300,
        draggable: true,
        resizable: false,
        buttons: {
            "Close": function() {
                $(this).dialog("close");
            }
        },
        position: ['center', 'top']
    });

    $('#free-shipping-link').click(function() {
        $('#free-shipping-window').dialog('open');
        return false;
    });

    $('#verisign-window').dialog({
        autoOpen: false,
        width: 425,
        height: 350,
        draggable: true,
        resizable: false,
        buttons: {
            "Close": function() {
                $(this).dialog("close");
            }
        },
        position: ['center', 'top']
    });

    $('#verisign-link').click(function() {
        $('#verisign-window').dialog('open');
        return false;
    });

    $('#instant-download-window').dialog({
        autoOpen: false,
        width: 330,
        draggable: true,
        resizable: false,
        buttons: {
            "Close": function() {
                $(this).dialog("close");
            }
        },
        position: ['center', 'top']
    });

    $('#instant-download-link').click(function() {
        $('#instant-download-window').dialog('open');
        return false;
    });

    var _DropDown = $("#SelectionDropDown");
    var _DownDownList = $("#SelectionItems");

    _DownDownList.css("display", "block").hide(1);

    _DropDown.click(function() {
        var _offset = $("#SelectionDropDown").offset();
        
        _DownDownList.css("top", _offset.top + $("#SelectionDropDown").height() - $("#DropDownRelative").offset().top).css("left", _offset.left - $("#DropDownRelative").offset().left);
        
        _DownDownList.slideToggle("normal", function() { 
            if($.browser.msie && $.browser.version < "7.0") {
                $(".Ie6Replaceable").css("display", (_DownDownList.is(":visible") ? "none" : ""));
            }
        });      
    });

    $(".SelectionItem").click(function() { 
        _DownDownList.slideToggle("normal", function() { 
            if($.browser.msie && $.browser.version < "7.0") {
                $(".Ie6Replaceable").css("display", (_DownDownList.is(":visible") ? "none" : ""));
            }
        });
    });
    
    var _index = 0;
    while(HideDetails(_index++));
    
    if((window.location.href.indexOf(":82") > -1 && window.location.href.indexOf("stats-click=true") > -1) || 
       (window.location.href.indexOf("localhost:1385") > -1 && window.location.href.indexOf("stats-click=true") > -1)) {
        var x = 0;
        var y = 0;
        var x2 = $("#canvas").offset().left;
        var y2 = $("#canvas").offset().top;
        var w = $().width();
        var h = $().height();
        var webid = "";

        //  grey the screen
        $("body").prepend("<div id=\"click-overlay\" style=\"height: 100%; width: 100%; z-index: 10; background-color: #666666; filter:alpha(opacity=75); opacity: 0.75; position: fixed; top: " + y + "px; left: " + x + "px; \"></div>");
        $("body").prepend("<div id=\"position-tracker\" style=\"padding: 2px 5px 2px 5px; font-size: 12px; z-index: 22; font-family: Arial; border: 1px solid #acacac; color: #ffffff; background-color: #cccccc; position: fixed; top: 0px; left: 0px;\"></div>");
        
        if(window.location.href.indexOf("&webid=" > -1)) {
            webid = GetParameterByName("webid");
            //alert(webid);
        }

        //  TODO: make request to server for all points on current page, loop through placing points
        ProductService.GetPageClicks(window.location.href, function(clicks) {
            _clicks = clicks;
            for(var i = 0; i < _clicks.length; i++) {
                //  place click points
                //alert(_clicks[i].WebID);
                if(webid != "") {
                    //  webid set, now only print those clicks out that the webid made
                    if(_clicks[i].WebID == webid) {
                        $("body").prepend("<div class=\"statsclick\" style=\"color: yellow; z-index: 20; background-color: #ffdddd; font-size: 10px; height: 5px; width: 5px; position: absolute; top: " + (y2+_clicks[i].Y-2)  + "px; left: " + (x2+_clicks[i].X-2) + "px; color: yellow;\"></div>");
                    }
                } else {
                    $("body").prepend("<div class=\"statsclick\" style=\"color: yellow; z-index: 20; background-color: #ffdddd; font-size: 10px; height: 5px; width: 5px; position: absolute; top: " + (y2+_clicks[i].Y-2)  + "px; left: " + (x2+_clicks[i].X-2) + "px; color: yellow;\"></div>");
                }
            }
        });
        
        $("body").prepend("<div id=\"ClickBox\" style=\"background: #eeeeee; font-family: Calibri; white-space: nowrap; display: none; border: solid 1px #ce0000; filter:alpha(opacity=75); opacity: 0.75; \">Testing!&nbsp;</div>");
        
        $().mousemove(
            function(e) {
                var x = (e.pageX - $("#canvas").offset().left);
                var y = (e.pageY - $("#canvas").offset().top);
                $("#position-tracker").html("X: " + x + "<br />Y: " + y);
            }
        );
        
        $().click(
            function(e) {
                var _clickBox = $("#ClickBox");
            
                if(!_boxTracking) {
                    _boxTracking = true;
                    
                    _clickBox
                        .css("display", "block")
                        .css("position", "absolute")
                        .css("z-index", "21")
                        .css("left", (_boxX = e.pageX) + 'px')
                        .css("top", (_boxY = e.pageY) + 'px');
                } else {
                    _boxTracking = false;
                }
            }
        ).mousemove(
            function(e) {
                var _clickBox = $("#ClickBox");
                
                if(!_boxTracking) return;
            
                _clickBox
                    .css("width", ((_boxX2 = e.pageX) - _boxX) + 'px')
                    .css("height",  ((_boxY2 = e.pageY) - _boxY) + 'px')
                    .html(GetClickCount());
            }
        );
    }
    
    $('a[rel="info"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
    
});

function GetParameterByName( name ) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}


function GetClickCount() {
    var _count = 0;
    var _offsetX = $("#canvas").offset().left;
    var _offsetY = $("#canvas").offset().top;
    
    for(var i = 0; i < _clicks.length; i++) {
        var _click = _clicks[i];
        var _x = _click.X + _offsetX;
        var _y = _click.Y + _offsetY;
        
        if(_x <= _boxX2 && _y <= _boxY2 && _x >= _boxX && _y >= _boxY)
            _count++;
    }
    
    return _count + " Clicks (" + parseInt(_count / _clicks.length * 10000) / 100 + "%)";
}

function trackclick(e, ca) {
    var x = (e.pageX - ca.offset().left);
    var y = (e.pageY - ca.offset().top);
    var l = window.location.href;
    var w = getCookie("webUserID");
    var s = getCookie("SessionID");
    //alert(x + ", " + y + ", " + l + ", " + w + ", " + s);
    try {
        ProductService.TrackClick(x, y, l, w, s);
    } catch(e) { }
}

function HideDetails(itemID) {
    if($("#remove" + itemID).length != 0 && $("#" + itemID + "detailsList").length == 0)
        return true;
        
    if($("#" + itemID + "detailsList").length == 0)
        return false;
        
    $("#" + itemID + "detailsList").hide(1);
    return true;
}

var _showMapDetails = ({ });

function toggleMapDetails(itemId) {
    _showMapDetails[itemId] = !_showMapDetails[itemId];
    
    if(_showMapDetails[itemId]) {
        $("#" + itemId + "detailsTitle").html("Hide Map Details <img class=\"mapdetailsarrows\" src=\"/images/cart/hide-map-details.gif\" />");
        $("#" + itemId + "detailsList").slideDown("normal");
    } else {
        $("#" + itemId + "detailsTitle").html("Show Map Details <img class=\"mapdetailsarrows\" src=\"/images/cart/view-map-details.gif\" />");
        $("#" + itemId + "detailsList").slideUp("normal");
    }
    return false;
}

function OpenInNewWindow( PageToOpen, WindowName, WindowWidth, WindowHeight, ScrollAbility, WindowStartPosition) {
    if(WindowStartPosition == "random") {
		LeftPosition = (screen.width) ? Math.floor(Math.random()*(screen.width - WindowWidth)) : 100;
		TopPosition = (screen.height) ? Math.floor(Math.random()*((screen.height - WindowHeight) - 75)) : 100;
	}
	
	if(WindowStartPosition == "center") {
		LeftPosition = (screen.width) ? (screen.width - WindowWidth) / 2 : 100;
		TopPosition = (screen.height) ? (screen.height - WindowHeight) / 2 : 100;
	} else if((WindowStartPosition != "center" && WindowStartPosition != "random") || WindowStartPosition == null) {
		LeftPosition = 0;
		TopPosition = 20;
	}
	
	WindowSettings = 'width=' + WindowWidth + ',height=' + WindowHeight + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + ScrollAbility + ',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win = window.open(PageToOpen, WindowName, WindowSettings);
}